Integration Testing Spring Controllers

Download Report

Transcript Integration Testing Spring Controllers

Lightning Talk by Ted Young
What is Integration Testing?
Unit Versus Integration Tests
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 Tests Aren’t Always Best
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();
}
Integration Testing Controllers
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
Testing a Controller
Controller
Servlet Container
Testing a Spring MVC Controller
Controller
Spring MVC
Servlet Container
Testing a Spring MVC Controller
Controller
Transactions
Spring
MVC
View Resolution
Servlet Container
Request
Mapping
Testing a Spring MVC Controller
Controller
Transactions
Spring
MVC
View Resolution
Request
Mapping
DispatcherServlet
Servlet Container
Mocking DispatcherServlet
DispatcherServlet
Mocking DispatcherServlet
DispatcherServlet
WebApplicationContext
Mocking DispatcherServlet
DispatcherServlet
WebApplicationContext
ServletConfig
ServletContext
Spring and JUnit
@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 {
...
}
View Technologies
How Many Use:
• JSPs
• Velocity
• Freemarker
• Facelets
An Example Test
@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);
}
Prepare and Review Model
@Test
public 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());
}
Test Validation
@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 Security
@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
@Test
public 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);
}
http://tedyoung.me
[email protected]