I have a controller that returns a list of sections. Each of these sections have a type. I want to test that the section of type "JOB" have a field "functions" with a size of 3.
Here is my code:
mockMvc.perform(get(url)
.contentType(MediaTypes.HAL_JSON)
.accept(MediaTypes.HAL_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.content",
hasSize(equalTo(2))))
.andExpect(jsonPath("$._embedded.content[?(@.type=='JOB')].functions",
hasSize(equalTo(3))));
The JSON returned by the controller (simplified for legibility):
{
"_embedded":{
"content":[
{
"subsections":null,
"type":"PROFILE",
"createdDate":null,
"lastModifiedDate":null
},
{
"functions":[
{
"rank":845131,
"function":"9IXZT"
},
{
"rank":82701,
"function":"T91WX"
},
{
"rank":98686,
"function":"PA7NA"
}
],
"type":"JOB",
"createdDate":null,
"lastModifiedDate":null
}
]
}
}
I tested the expression with this tool against that JSON and works correctly, but when running the test I'm getting this assertion error:
java.lang.AssertionError: JSON path "$._embedded.content[?(@.type=='JOB')].functions"
Expected: a collection with size <3>
but: collection size was <1>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:86)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
...