We will take the example of the Spring MVC from this chapter as a target application to test and execute unit testing. We have the EmployeeController
class as a target class to test.
You'll find the following code in EmployeeController.java
:
package org.packt.Spring.chapter7.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/employee") public class EmployeeController { @RequestMapping(method = RequestMethod.GET) public String welcomeEmployee(ModelMap model) { model.addAttribute("name", "Hello World!"); model.addAttribute("greetings", "Welcome to Packt Publishing - Spring MVC !!!"); return "hello"; } }
In the aforementioned code snippet, the welcomeEmployee()
method in the EmployeeController
class gets mapped into the HTTP request. In the welcomeEmployee()
method, the request is processed and bound to the model objects. Then, the EmployeeController
class updates the model and the view state, and after this it returns to the logical view.
The main objective of the unit testing controller class is to verify that the methods of the controller class update the model and the view states properly and also return to the correct view. Since we perform the testing of the controller class's behavior, we should mock the service layer (if present) with the correct behavior.
For the EmployeeController
class, we would like to develop the test cases for the welcomeEmployee()
method. Here we will test the welcomeEmployee()
method of the controller using JUnit 4.
It is important to note that the classes undergoing testing should be placed in the folder /src/test/java
and the resources filed should be placed in the folder /src/test/resources
.
You'll find this code in EmployeeControllerTest.java
:
package org.packt.Spring.chapter7.springmvc.controller; import org.junit.Assert; import org.junit.Test; WelcomeEmployee import org.packt.Spring.chapter7.springmvc.controller.EmployeeController; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ModelMap; public class EmployeeControllerTest { @Test public void test () { EmployeeController controller = new EmployeeController(); ModelMap modelMap = new ExtendedModelMap(); String view = controller.welcomeEmployee(modelMap); // verify view page name Assert.assertNotNull(view); Assert.assertEquals("hello", view); // verify page title String titlename = modelMap.get("name").toString(); Assert.assertEquals("Hello World!", titlename); // verify greeting message String greetings = modelMap.get("greetings").toString(); Assert.assertEquals("Welcome to Packt Publishing - Spring MVC !!!", greetings); } }
Even though the preceding code works, it has the following problems:
GET
, POST
, PUT
, or DELETE
ModelMap
It is always challenging to perform unit testing of web applications. A better solution for the aforementioned problem is provided by the Spring MVC test framework, which allows us to test the Spring MVC controller.