[英]Spring MVC Test empty json
How can I test for an empty JSON response from a rest endpoint.如何测试来自休息端点的空 JSON 响应。 I was hoping for something along the lines:我希望有类似的东西:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(jsonPath("$", empty()));
obviously this fails as {}
is not exactly empty.显然这会失败,因为{}
并不完全为空。 Any advice?有什么建议吗?
Try this one:试试这个:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(content().string("[]"));
This worked for me:这对我有用:
ResultActions actions = mockMvc.perform(..);
actions.andExpect(content().string(""));
In JSON, {}
is a non-null, but empty, object.在 JSON 中, {}
是一个非空但为空的对象。 This translates into Java as an empty map.这会将 Java 转换为空映射。 You can validate that a JSON element is non-null and empty like this:您可以像这样验证 JSON 元素是否为非 null 且为空:
actions.andExpect(jsonPath("$").value(Collections.EMPTY_MAP))
exists()
方法对我来说效果很好。
this.mockMvc.perform(get("....")).andExpect(jsonPath("$").exists());
This can be tested using an empty map provided with the "jsonpathresultmatchers".这可以使用“jsonpathresultmatchers”提供的空地图进行测试。
ResultActions actions = mockMvc.perform(..);
actions.andExpect(jsonPath("$", anEmptyMap()));
.andExpect(jsonPath("$").isEmpty()); .andExpect(jsonPath("$").isEmpty());
is working for me.正在为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.