SpringMVC/MockMVC/ jsonpath compare list of Integers

Viewed 859

I have searched online but can't find why or how to fix it.

Problem:

My JUnit test pulls a list of user id's from a REST-API. However, I can't seem to assert the contents of the response...

API Response:

[
  {"userId":1458548702},
  {"userId":1458548703},
  {"userId":1458548704},
  {"userId":1458548706},
  {"userId":1458548707}
]

Assertion Code:

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

Error Message:

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

Libraries I'm using:

  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 Answers
Related