lightning talk by ted young. what is integration testing?

28
Lightning Talk by Ted Young Integration Testing Spring Controllers

Upload: sammy-eachus

Post on 30-Mar-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lightning Talk by Ted Young. What is Integration Testing?

Lightning Talk by Ted Young

Integration Testing Spring Controllers

Page 2: Lightning Talk by Ted Young. What is Integration Testing?

What is Integration Testing?

Page 3: Lightning Talk by Ted Young. What is Integration Testing?

Unit Test Integration Test

Isolation Entire Stack

Unit Versus Integration Tests

Page 4: Lightning Talk by Ted Young. What is Integration Testing?

Unit Test Integration Test

Isolation Entire Stack

Inject Mocks Inject Implementations

Unit Versus Integration Tests

Page 5: Lightning Talk by Ted Young. What is Integration Testing?

Unit Test Integration Test

Isolation Entire Stack

Inject Mocks Inject Implementations

Verifies Code Verifies Application

Unit Versus Integration Tests

Page 6: Lightning Talk by Ted Young. What is Integration Testing?

public void persist(Foo foo) {entityManager.persist(foo);

}

public List<Foo> find() {return entityManager

.createQuery("from Foo").getResultList();}

public List<Foo> findByName(String name) {CriteriaBuilder cb = entityManager.getCriteriaBuilder();CriteriaQuery<Foo> query = cb.createQuery(Foo.class);Root<Foo> root = query.from(Foo.class);query.where(cb.equal(root.get(Foo_.name), name));return entityManager.createQuery(query).getResultList();

}

Unit Tests Aren’t Always Best

Page 7: Lightning Talk by Ted Young. What is Integration Testing?

External Tool (e.g. JMeter) Test Harness (e.g. JUnit)

External Tool IDE, Maven, CI, etc.

Integration Testing Controllers

Page 8: Lightning Talk by Ted Young. What is Integration Testing?

External Tool (e.g. JMeter) Test Harness (e.g. JUnit)

External Tool IDE, Maven, CI, etc.

Script Actions Build Requests in Java

Integration Testing Controllers

Page 9: Lightning Talk by Ted Young. What is Integration Testing?

External Tool (e.g. JMeter) Test Harness (e.g. JUnit)

External Tool IDE, Maven, CI, etc.

Script Actions Build Requests in Java

No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring

Integration Testing Controllers

Page 10: Lightning Talk by Ted Young. What is Integration Testing?

External Tool (e.g. JMeter) Test Harness (e.g. JUnit)

External Tool IDE, Maven, CI, etc.

Script Actions Build Requests in Java

No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring

Refactor = Rewrite Make Use of IDE Tools

Integration Testing Controllers

Page 11: Lightning Talk by Ted Young. What is Integration Testing?

External Tool (e.g. JMeter) Test Harness (e.g. JUnit)

External Tool IDE, Maven, CI, etc.

Script Actions Build Requests in Java

No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring

Refactor = Rewrite Make Use of IDE Tools

Errors at Runtime Errors at Compiletime

Integration Testing Controllers

Page 12: Lightning Talk by Ted Young. What is Integration Testing?

Testing a Controller

Servlet Container

Controller

Page 13: Lightning Talk by Ted Young. What is Integration Testing?

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

Page 14: Lightning Talk by Ted Young. What is Integration Testing?

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

View Resolution

Transactions Request Mapping

Page 15: Lightning Talk by Ted Young. What is Integration Testing?

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

View Resolution

Transactions Request Mapping

DispatcherServlet

Page 16: Lightning Talk by Ted Young. What is Integration Testing?

Mocking DispatcherServlet

DispatcherServlet

Page 17: Lightning Talk by Ted Young. What is Integration Testing?

Mocking DispatcherServlet

DispatcherServlet

WebApplicationContext

Page 18: Lightning Talk by Ted Young. What is Integration Testing?

Mocking DispatcherServlet

DispatcherServlet

WebApplicationContext

ServletConfig ServletContext

Page 19: Lightning Talk by Ted Young. What is Integration Testing?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:spring.xml")

public class SomeControllerTests {

...

}

Spring and JUnit

Page 20: Lightning Talk by Ted Young. What is Integration Testing?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:spring.xml",

loader=MockWebApplicationContextLoader.class)

public class SomeControllerTests {

...

}

Spring and JUnit

Page 21: Lightning Talk by Ted Young. What is Integration Testing?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:spring.xml",

loader=MockWebApplicationContextLoader.class)

@MockWebApplication(name="some-controller",webapp="/src/main/webapp")

public class SomeControllerTests {

...

}

Spring and JUnit

Page 22: Lightning Talk by Ted Young. What is Integration Testing?

How Many Use:

• JSPs

• Velocity

• Freemarker

• Facelets

View Technologies

Page 23: Lightning Talk by Ted Young. What is Integration Testing?

@Autowired

private DispatcherServlet servlet;

@Autowired

private SomeRepository repository;

@Test

public void viewTest() throws Exception {

MockHttpServletRequest request =

new MockHttpServletRequest("GET", "/view");

request.addParameter("id", "0");

MockHttpServletResponse response =

new MockHttpServletResponse();

servlet.service(request, response);

String results = response.getContentAsString().trim();

Assert.assertEquals(

"<html><body>Hello World!</body></html>",

results);

}

An Example Test

Page 24: Lightning Talk by Ted Young. What is Integration Testing?

@Testpublic void saveTest() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");

request.addParameter("name", "Ted");

MockHttpServletResponse response = new MockHttpServletResponse();

servlet.service(request, response);

Assert.assertEquals("Ted", repository.find(1).getName());}

Prepare and Review Model

Page 25: Lightning Talk by Ted Young. What is Integration Testing?

@Test(expected=NestedServletException.class)public void saveFailedTest() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");

request.addParameter("name", "");

MockHttpServletResponse response = new MockHttpServletResponse();

servlet.service(request, response);}

Test Validation

Page 26: Lightning Talk by Ted Young. What is Integration Testing?

@Test(expected=NestedServletException.class)public void secureFailedTest() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest("GET", "/secure/view");

MockHttpServletResponse response = new MockHttpServletResponse();

servlet.service(request, response);}

Test Security

Page 27: Lightning Talk by Ted Young. What is Integration Testing?

@Testpublic void secureTest() throws Exception {

SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(

"Ted", "password"));

MockHttpServletRequest request = new MockHttpServletRequest("GET", "/secure/view");

MockHttpServletResponse response = new MockHttpServletResponse();

servlet.service(request, response);

String results = response.getContentAsString().trim();

Assert.assertEquals("<html><body>Hello Ted!</body></html>",results);

}

Test Security

Page 28: Lightning Talk by Ted Young. What is Integration Testing?

http://tedyoung.me

[email protected]

Please Visit My Site