4. - Build web applications with Spring Web MVC
- Familiar with mock objects unit testing (JUnit and Mockito)
4
Assumptions on Audience
5. Spring Controllers Testing
- Example Domain Model
- Subject Under Test
- Pure Unit Tests
Spring MVC Test Framework
- Standalone Server-Side Integration Tests
- Web Application Context Server-Side Integration Tests
Further materials
Q&A
5
Agenda
17. ChannelController behaviour
- Positive test case
When we are looking for an existent channel by its id
- Negative test case
When we are looking for an absent channel by its id
17
What we are going to test
22. 22
Unit Test Scaffolding
!
public class ChannelControllerTest {
@Mock
private ChannelRepository channelRepository;
!
@InjectMocks
private ChannelController channelController =
// optional new ChannelController();
}
24. 24
Unit Test Scaffolding
@RunWith(MockitoJUnitRunner.class)
public class ChannelControllerTest {
@Mock
private ChannelRepository channelRepository;
!
@InjectMocks
private ChannelController channelController =
// optional new ChannelController();
!
!
@Mock
private Channel channel; // dummy
// test cases go here
}
26. 26
Positive Test Case
@Test
public void itShouldFindChannel() {
when(channelRepository.findOne(1))
.thenReturn(channel);
!
assertThat(
channelController.getChannel(1), is(channel)
);
}
27. 27
Negative Test Case
@Test
public void itShouldNotFoundChannel() {
// optional
when(channelRepository.findOne(-1))
.thenReturn(null);
}
28. 28
Negative Test Case
@Test(expected = ChannelNotFoundException.class)
public void itShouldNotFoundChannel() {
// optional
when(channelRepository.findOne(-1))
.thenReturn(null);
!
channelController.getChannel(-1);
}
29. - Easy to write
- Incredibly fast (a few milliseconds per test case)
29
Pros
30. - Can use Spring mocks from org.springframework.mock.web
- MockHttpServletRequest/Response/Session
- MockMultipartFile
- MockFilterChain
…
- ModelAndViewAssert from org.springframework.test.web
to apply asserts on a resulting ModelAndView
30
Additional Capabilities on Demand
31. - A lot left untested
- Request mappings
- Type conversion
- Transactions
- Data binding
- Validation
- Filters
- …
- No Spring annotations used
- No DispatcherServlet interactions
- No actual Spring MVC configuration loaded
31
Caveats
36. - Response status, headers, content
- Spring MVC and Servlet specific results
- Model, flash, session, request attributes
- Mapped controller method
- Resolved exceptions
- Various options for asserting the response body
- JsonPath, XPath, XMLUnit
36
What can be tested
37. - Almost all template technologies are supported
- JSON, XML, Velocity, Freemarker, Thymeleaf, PDF etc.
- Except JSP (because it relies on Servlet Container)
- you can assert only on the selected JSP view name
- No actual redirecting or forwarding
- you can assert the redirected or forwarded URL
37
Testing View Layer
38. Standalone setup for testing one individual controller at a time
without actual Spring MVC configuration loading
38
MockMvc “Standalone” Setup
private ChannelController controller = //...
!
private MockMvc mockMvc;
!
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.build();
}
44. - MockMvcBuilders.* to set up MockMvc instances
- MockMvcRequestBuilders.* to create requests
- MockMvcResultMatchers.* for request result assertions on
44
Useful Static Imports
61. ChannelController instantiated
Mock of ChannelRepository injected
MockMvc was set-upped
MockHttpServletRequest prepared
Executed via DispatcherServlet
Assertions applied on the resulting MockHttpServletResponse
Assertions applied on the resulting MvcResult
61
What happened
62. - Easy to write
- Uses Spring annotations
- Always interacts with DispatcherServlet
62
Pros
63. - A bit slow (about 1 second for the first test case)
- No Actual Spring MVC configuration loaded
63
Caveats
75. Actual Web MVC application context loaded
MockHttpServletRequest prepared
Executed via DispatcherServlet
Assertions applied on the resulting MockHttpServletResponse
Assertions applied on the resulting MvcResult
75
What happened
76. - Easy to write
- Loads actual Spring MVC configuration (cacheable)
- Uses Spring annotations
- Always Interacts with DispatcherServlet
76
Pros
77. - Slower than the “Standalone” option (depends on amount of beans
in a particular Spring Mvc configuration)
- Does not replace end-to-end testing like Selenium
77
Caveats
79. Integration between Spring MVC Test Framework and HtmlUnit.
Repository: https://github.com/spring-projects/spring-test-htmlunit
Documentation: https://github.com/spring-projects/spring-test-
htmlunit/blob/master/src/asciidoc/index.adoc
79
Spring MVC Test HtmlUnit
81. Webinar: Testing Web Applications with Spring 3.2
by Sam Brannen (Swiftmind) and Rossen Stoyanchev
https://www.youtube.com/watch?v=K6x8LE7Qd1Q
Spring Testing
by Mattias Severson
https://www.youtube.com/watch?v=LYVJ69h76nw
!
81
Videos