Spring mvc SpringMVC/MockMVC/jsonpath整数比较列表
我在网上搜索过,但找不到原因或如何修复它 问题:Spring mvc SpringMVC/MockMVC/jsonpath整数比较列表,spring-mvc,jsonpath,mockmvc,Spring Mvc,Jsonpath,Mockmvc,我在网上搜索过,但找不到原因或如何修复它 问题: [ {"userId":1458548702}, {"userId":1458548703}, {"userId":1458548704}, {"userId":1458548706}, {"userId":1458548707} ] List<Integer> expectedIds = .... ResultActions result = ... //JSONArray and expectedIds ar
[
{"userId":1458548702},
{"userId":1458548703},
{"userId":1458548704},
{"userId":1458548706},
{"userId":1458548707}
]
List<Integer> expectedIds = ....
ResultActions result = ...
//JSONArray and expectedIds are the same length
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//Below Fails:
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$[*].userId",
Matchers.containsInAnyOrder(expectedIds)
)
);
我的JUnit测试从REST-API中提取一个用户id列表。
然而,我似乎无法断言响应的内容
API响应:
[
{"userId":1458548702},
{"userId":1458548703},
{"userId":1458548704},
{"userId":1458548706},
{"userId":1458548707}
]
List<Integer> expectedIds = ....
ResultActions result = ...
//JSONArray and expectedIds are the same length
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//Below Fails:
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$[*].userId",
Matchers.containsInAnyOrder(expectedIds)
)
);
断言代码:
[
{"userId":1458548702},
{"userId":1458548703},
{"userId":1458548704},
{"userId":1458548706},
{"userId":1458548707}
]
List<Integer> expectedIds = ....
ResultActions result = ...
//JSONArray and expectedIds are the same length
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//Below Fails:
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$[*].userId",
Matchers.containsInAnyOrder(expectedIds)
)
);
List expectedds=。。。。
ResultActions结果=。。。
//JSONArray和ExpectedId的长度相同
结果与预期(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//以下失败:
结果与预期(
MockMvcResultMatchers.jsonPath(
“$[*].userId”,
Matchers.ContainesAnyOrder(ExpectedId)
)
);
错误消息:
java.lang.AssertionError: JSON path "$[*].userId"
Expected: iterable over [<[1458548702, 1458548703, 1458548704, 1458548706, 1458548707]>] in any order
but: Not matched: <1458548702>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
...
java.lang.AssertionError:JSON路径“$[*].userId”
预期值:iterable超过[]的任意顺序
但是:不匹配:
位于org.hamcrest.matcherasert.assertThat(matcherasert.java:20)
...
我正在使用的库:
[
{"userId":1458548702},
{"userId":1458548703},
{"userId":1458548704},
{"userId":1458548706},
{"userId":1458548707}
]
List<Integer> expectedIds = ....
ResultActions result = ...
//JSONArray and expectedIds are the same length
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//Below Fails:
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$[*].userId",
Matchers.containsInAnyOrder(expectedIds)
)
);