[英]How to write a test to send json to a @RequestBody method by spring mvc test
[英]Spring MVC Test empty json
如何測試來自休息端點的空 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(""));
在 JSON 中, {}
是一個非空但為空的對象。 這會將 Java 轉換為空映射。 您可以像這樣驗證 JSON 元素是否為非 null 且為空:
actions.andExpect(jsonPath("$").value(Collections.EMPTY_MAP))
exists()
方法對我來說效果很好。
this.mockMvc.perform(get("....")).andExpect(jsonPath("$").exists());
這可以使用“jsonpathresultmatchers”提供的空地圖進行測試。
ResultActions actions = mockMvc.perform(..);
actions.andExpect(jsonPath("$", anEmptyMap()));
.andExpect(jsonPath("$").isEmpty());
正在為我工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.