控制器就像:
@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);
}
我找不到使用Spring mvc控制器测试ResponseEntity的方法。有人可以帮我这个吗?
答案 0 :(得分:3)
在Spring集成测试框架中,它为测试控制器提供了类MockMvc。
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.