Spring MVC controller testing using the Spring TestContext framework

Mockito facilitates developers to create mock objects of the DAO layer as we saw in the preceding section. We did not have the DAO object; however, even without it, the testing was made possible. Spring MVC layer testing without mock objects is not possible, as they are highly dependent upon the request and response object that gets initialized by the container. The spring-test module supports creation of a mock object for the Servlet API, which tests the web component without deploying them in actual container. The following table shows the list of packages that are provided by the Spring TestContext framework for mock creation:

Package name Provides mock implementation of
org.springframework.mock.env Environment and Property Source
org.springframework.mock.jndi JNDI SPI
org.springframework.mock.web Servlet API
org.springframework.mock.portlet Portlet API

The org.springframework.mock.web package provides the MockHttpServletRequest, MockHttpServletResponse, and MockHttpSession as mock objects for HttpServeltRequest, HttpServletResponse, and HttpSession for use. It also provides the ModelAndViewAssert class to test the ModelAndView objects from the Spring MVC framework. Let's test our SearchBookController discussed in the ReadMyBooks application step by step:

  1. Add spring-test-5.0.0.RC1.jar to the ReadMyBooks application, which we will use for testing.
  2. Create a com.packt.ch06.controllers.test_controllers package to add test cases for the controllers.
  3. Create TestSearchBookController as a JUnit case in the package created in the earlier step.
  4. Annotate it by @WebAppConfiguration.
  5. Declare data members of type SearchBookController, and autowire it as follows:
        @WebAppConfiguration 
@ContextConfiguration({ "file:WebContent/WEB-INF/book-
servlet.xml" })
@RunWith(value = SpringJUnit4ClassRunner.class)
public class TestSearchBookController {
@Autowired
SearchBookController searchBookController;
}
  1. Let's add testSearchBookByAuthor() to test the searchBookByAuthor() method. The method accepts the author's name entered by the user in the web form, and returns a list of books written by the author. The code will be written like this:
    1. Initialize the data required by the method under testing.
    2. Invoke the method under test.
    3. Assert the values.
  1. The final code will be as follows:
        @Test 
public void testSearchBookByAuthor() {
String author_name="T.M.Jog";
ModelAndView modelAndView=
searchBookController.searchBookByAuthor(author_name);
assertEquals("display",modelAndView.getViewName());
}

We are testing the name of the display view, which was written from the controller method.

  1. The Spring framework facilitates ModelAndViewAssert, which provides the method to test ModelAndView returned by the controller method, as shown in the following code:
        @Test 
public void test SearchBookByAuthor_New()
{
String author_name="T.M.Jog";
List<Book>books = newArrayList<Book>();
books.add(new Book("Learning Modular Java Programming",
9781235, "packt pub publication", 800,
"explore the power of modular Programming ",
author_name));
books.add(new Book("Learning Modular Java Programming",
9781235, "packt pub publication", 800,
"explore the power of modular Programming ",
author_name));
ModelAndView modelAndView=
searchBookController.searchBookByAuthor(author_name);
ModelAndViewAssert.assertModelAttributeAvailable(
modelAndView, "book_list");
}
  1. Execute the test case; the green color indicates that the test case has passed.

We successfully tested SearchBookController, which has easy coding without any form submission, form-model attribute binding, form validation, and so on. Such complex code testing becomes more complex with the mock objects that we just handled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221