Spring Release 3.2 - Easier Spring MVC Tests
Introduction
I've been learning how to use the Spring MVC web application framework as I contemplate switching from Struts 2 to Spring MVC. In December 2012, Spring announced the release of version 3.2. One of the new features in this release is easier testing of Spring MVC applications (which is a weak area for Struts 2). So this article provides a simple Spring MVC example project that includes JUnit tests of the Spring MVC controller class.
Example Project
This project is an example of how to do integration testing of a Spring MVC project by using the new MockMvc class provided in Spring 3.2. This project is initially based on the Simple-Spring-Web-Application project provided by J. Verstry on GitHub at https://github.com/JVerstry/Web-Related-Examples. The updated project with the JUnit tests is available in my GitHub repository at https://github.com/phillips1021/phillips_spring_examples, see project baremvc_with_spring_mvc_unit_test.
I added to J. Verstry's original Spring MVC project JUnit tests that demonstrate how to test the Spring MVC controller class HomeController.java. All new new code is in src/test/java and src/test/resources.
To run the tests use mvn -e clean test in the project's root folder in a terminal (command) window. To run the web application in Tomcat use mvn -e clean tomcat7:run.
Once Tomcat has started up you can use these URLs:
http://localhost:9090/baremvc
http://localhost:9090/baremvc/compare?input1=Bruce&input2=Andrew
to run the web application.
Test Class
Here is the code for the test class (minus all the import statements).
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("test-servlet-context.xml")
public class HomeControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testDefault() throws Exception {
mockMvc.perform( get("/")).andExpect(status().isOk() ).andExpect(forwardedUrl("WEB-INF/views/home.jsp"));
}
@Test
public void testCompare() throws Exception {
mockMvc.perform( get("/compare?input1=Bruce&input2;=Andrew")).andExpect(status().isOk())
.andExpect(model().attributeExists("output") ).andExpect(forwardedUrl("WEB-INF/views/compareResult.jsp"));
}
}
Once you have the MockMvc object setup you can use it to perform a mock HTTP request and then have it execute one or more addExpect methods that test the result of making the request. For example, in method testDefault, the test is making a request to the application's root ("/") and it expects to get back an HTTP status of OK (200) and that the view the user's browser will be forwarded to is "WEB-INF/views/home.jsp". The method testCompare is similar but includes checking that an object named "output" exists on the model (and therefore available to the view page).
Once you understand how to use the perform and addExpect method calls and the various arguments you can provide those calls you can create sophisticated tests. Be sure to review the JavaDoc for classes MockMvcRequestBuilders and MockHttpServletResponse for more information about arguments you can provide to the perform and andExpect methods.
Summary
I'm definately not an expert on Spring MVC and unit testing. But I think it says alot about the Spring framework in that it did not take me long to figure out how to add unit tests to a Spring MVC project. Consult the references below for more information on testing your Spring MVC classes.
References:
- Spring MVC Test Framework, Spring Documentation for Release 3.2, http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework
- JavaDoc for Class MockMvc, http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/testMockMvc.html
- JavaDoc for Interface ResultActions, http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/testResultActions.html
- JavaDoc for Class MockMvcRequestBuilders, http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/testrequest/MockMvcRequestBuilders.html
- JavaDoc for Class MockHttpServletResponse, http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/mock/web/MockHttpServletResponse.html
There are no comments for this entry.
[Add Comment]