integration testing spring controllers

Post on 03-Jan-2016

37 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Integration Testing Spring Controllers. Lightning Talk by Ted Young. What is Integration Testing?. Unit Versus Integration Tests. Unit Versus Integration Tests. Unit Versus Integration Tests. Unit Tests Aren’t Always Best. public void persist(Foo foo) { entityManager .persist (foo ); } - PowerPoint PPT Presentation

TRANSCRIPT

Lightning Talk by Ted Young

Integration Testing Spring Controllers

What is Integration Testing?

Unit Test Integration Test

Isolation Entire Stack

Unit Versus Integration Tests

Unit Test Integration Test

Isolation Entire Stack

Inject Mocks Inject Implementations

Unit Versus Integration Tests

Unit Test Integration Test

Isolation Entire Stack

Inject Mocks Inject Implementations

Verifies Code Verifies Application

Unit Versus Integration Tests

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

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

External Tool IDE, Maven, CI, etc.

Integration Testing Controllers

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

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

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

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

Testing a Controller

Servlet Container

Controller

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

View Resolution

Transactions Request Mapping

Testing a Spring MVC Controller

Servlet Container

Controller

Spring MVC

View Resolution

Transactions Request Mapping

DispatcherServlet

Mocking DispatcherServlet

DispatcherServlet

Mocking DispatcherServlet

DispatcherServlet

WebApplicationContext

Mocking DispatcherServlet

DispatcherServlet

WebApplicationContext

ServletConfig ServletContext

@RunWith(SpringJUnit4ClassRunner.class)

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

public class SomeControllerTests {

...

}

Spring and JUnit

@RunWith(SpringJUnit4ClassRunner.class)

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

loader=MockWebApplicationContextLoader.class)

public class SomeControllerTests {

...

}

Spring and JUnit

@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

How Many Use:

• JSPs

• Velocity

• Freemarker

• Facelets

View Technologies

@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

@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

@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

@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

@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

http://tedyoung.me

mail@tedyoung.me

Please Visit My Site

top related