我如何测试来自其他端点的空JSON响应。我一直希望有类似的东西:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(jsonPath("$", empty()));
显然,这失败了,因为{}
并非完全为空。有什么建议吗?
尝试这个:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(content().string("[]"));
这对我有用:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(content().string(""));
exists()
方法对我来说效果很好。
this.mockMvc.perform(get("....")).andExpect(jsonPath("$").exists());
在JSON中,{}
是非空但为空的对象。这将转换为Java,成为一个空映射。您可以像这样验证JSON元素为非空值:
.andExpect(jsonPath("$.somePath").value(Collections.EMPTY_MAP))