I have a rest json response that contains an array and I would like to write tests for it. Sadly, it comes unordered so I can't rely on a specific order of the array's content. How should I test the matching information together?
It looks like something like this:
{
"context": "school",
"students": [
{
"id": "1",
"name": "Alice",
"address": {
"city": "London",
"street": "Big Street 1"
}
},
{
"id": "2",
"name": "Bob",
"address": {
"city": "Manchester",
"street": "Little Street 2"
}
}
]
}
I have tried this so far:
//...
getMockMvc().perform(get("/students"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.students[*].id", containsInAnyOrder("1, 2")))
.andExpect(jsonPath("$.students[*].name", containsInAnyOrder("Alice", "Bob")))
.andExpect(jsonPath("$students[*].address.city", containsInAnyOrder("London", "Manchester");
// ... other parts of code
The problem is that my students
array can be in any order. And now this solution does not make sure that I can test the cohesive information together.
How should I achieve this?