How to test Spring mvc controller tests for response entity?
gson, java, spring-mvc
Solution
In spring integration test framework, it provides class MockMvc to test controllers.
MockMvc mvc = MockMvcBuilders.webAppContextSetup(wac).build(); // was is a web application context.
MvcResult result = mvc
.perform(
get("/autocomplete")
.accept(
MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
String content = result.getResponse().getContentAsString(); // verify the response string.
Problem
The controller is like: ``` @RequestMapping(method = RequestMethod.GET, value = "/autocomplete") public ResponseEntity<String> autoComplete(@RequestParam("query") final String searchKey) { List<String> list = ... Gson gson = new Gson(); String jsonString = gson.toJson(list); return new ResponseEntity<String>(jsonString, HttpStatus.OK); } ``` I couldn't find a way to test the ResponseEntity using Spring mvc controller. Could anyone help me with this?