SpringMVC / MockMVC / jsonpath比较整数列表

时间:2016-03-21 08:52:23

标签: spring-mvc jsonpath mockmvc

我在线搜索但找不到原因或如何修复它。

问题:

我的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)
  )
);

错误讯息:

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)
...

我正在使用的库:

  1. com.jayway.jsonpath json-path 2.0.0
  2. com.jayway.jsonpath json-path-assert 2.0.0
  3. org.mockito mockito-core 1.10.19
  4. org.hamcrest hamcrest-all 1.3

0 个答案:

没有答案