Spring MockMvc: match a collection of JSON objects in any order

Additionally there is another way to assert the json without being strict about order using MockMvcResultMatchers

.andExpect(MockMvcResultMatchers.content().json(<json-here>, false))

By setting the strict=false, it can do a fussy search.


You can assert list items fields ignoring order:

.andExpect(jsonPath("$[*].id", containsInAnyOrder("321", "123")))
.andExpect(jsonPath("$[*].created", containsInAnyOrder("2019-03-01", "2019-03-02")))
.andExpect(jsonPath("$[*].updated", containsInAnyOrder("2019-03-15", "2019-03-16")))

Another approach would be to check that specific list items exist in response:

.andExpect(jsonPath("$.[?(@.id == 123 && @.created == \"2019-03-02\" && @.updated == \"2019-03-16\")]").exists())
.andExpect(jsonPath("$.[?(@.id == 321 && @.created == \"2019-03-01\" && @.updated == \"2019-03-15\")]").exists())