我正在测试一个返回地图的控制器
@RequestMapping("/")
@ResponseBody
public Map<String, String> getMessages(@RequestBody String foo) {
Map<String, String> map = boo.getMap(foo);
return map;
}
测试:
...
resultActions
.andDo(print())
.andExpect(status().isOk())
.andExpect(
content().contentTypeCompatibleWith(
MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath(EXPRESSION, equalsTo(foo));
...
我应该使用哪个表达式从 Map 中读取键和值?
编辑:解决它的一种方法可能是:
MvcResult result = resultActions.andReturn();
MockHttpServletResponse response = result.getResponse();
String content = response.getContentAsString();
Gson gson = new Gson();
Type typeOfT = new TypeToken<Map>() {
}.getType();
Map<String, String> map = gson.fromJson(content, typeOfT);
然后遍历地图检查值。但是有没有办法做到这一点jsonPath
?