Spring MVC Test empty json

问题描述 投票:8回答:4

我如何测试来自其他端点的空JSON响应。我一直希望有类似的东西:

    ResultActions actions = mockMvc.perform(..);
    actions.andExpect(jsonPath("$", empty()));

显然,这失败了,因为{}并非完全为空。有什么建议吗?

json spring-test
4个回答
10
投票

尝试这个:

 ResultActions actions = mockMvc.perform(..);
 actions.andExpect(content().string("[]"));

3
投票

这对我有用:

ResultActions actions = mockMvc.perform(..);
actions.andExpect(content().string(""));

0
投票

exists()方法对我来说效果很好。

this.mockMvc.perform(get("....")).andExpect(jsonPath("$").exists());

0
投票

在JSON中,{}是非空但为空的对象。这将转换为Java,成为一个空映射。您可以像这样验证JSON元素为非空值:

.andExpect(jsonPath("$.somePath").value(Collections.EMPTY_MAP))

© www.soinside.com 2019 - 2024. All rights reserved.