The design behind the Spring MVC test is to test the controller by performing actual requests and generating responses, as they would be at runtime. MockMvc is used to mock the servlet container, and it can perform a request and verify the resulting response status and response elements. We'll build a Spring controller to generate a JSON response as in the case of a rest controller and then use MockMvc to unit test the request and the response:
Employee
POJO class that holds employee information, such as ID, name, and salary./employees/{id}
and /employees/ urls
. We'll create a HashMap and store dummy employees. The following is the class:@Controller public class HRController { private Map<Integer, Employee> database = new HashMap<Integer, Employee>(); public HRController() { loadDummyData(); } private void loadDummyData() { Employee john = new Employee(); john.setId(1); john.setName("John Doe"); john.setSalary(100.00); database.put(1, john); Employee karen = new Employee(); karen.setId(2); karen.setName("Karen Cushing"); karen.setSalary(500.00); database.put(2, karen); } @RequestMapping(value = "/employees/{id}", method = RequestMethod.GET) public @ResponseBody Employee retrieve(@PathVariable int id) { return database.get(id); } @RequestMapping(value = "/employees", method = RequestMethod.GET) public @ResponseBody List<Employee> retrieveAll() { return new ArrayList<Employee>(database.values()); } }
Note that the retrieve
and retrieveAll
methods are annotated with @RequestMapping(value = "/employees/{id}", method = RequestMethod.GET)
and @RequestMapping(value = "/employees", method = RequestMethod.GET)
, respectively to map the URLs. Also, both the methods are annotated with an @ResponseBody
annotation to return response as JSON object.
http://localhost:8080/SpringWebTest/employees/
URL, the following JSON output is displayed:http://localhost:8080/SpringWebTest/employees/1
, this is how the output looks:org.springframework.test.web.servlet.MockMvc, org.springframework.test.web.servlet.request.MockMvcRequestBuilders
, and org.springframework.test.web.servlet.result.MockMvcResultMatchers
classes. The following JUnit test demonstrates the usages of MockMvc
:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations ="classpath:beans.xml") @WebAppConfiguration public class HRControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup (this.wac).build(); } @Test public void getEmployee() throws Exception { this.mockMvc.perform(get("/employees/1"). accept(MediaType.parseMediaType( "application/json;charset=UTF-8"))) .andExpect(status().isOk()) .andExpect(content().contentType( "application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("John Doe")) .andExpect(jsonPath("$.salary").value(100.00)) .andExpect(jsonPath("$.id").value(1)); } }
The MockMvcBuilders
class needs a WebApplicationContext
to build a MockMvc
object; the WebApplicationContext
is autowired using the @WebAppConfiguration
annotation. The MockMvc
object is used to perform a GET
request to /employees/1
and then it verifies that the response status is 200 (isOk()
) as well as the JSON response. The jsonPath("$.name").value("John Doe")
statement checks whether the output JSON contains a name field and its value is John Doe
. So, we just bypassed the servlet container to test the real request/response handling.
18.118.37.154