JsonPath expression with filters in Spring MVC test
<p>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.</p>

<p>Here is my code:</p>

<pre><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))));
</code></pre>

<p>The JSON returned by the controller (simplified for legibility):</p>

<pre><code>{
   "_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
         }
      ]
   }
}
</code></pre>

<p>I tested the expression with <a href="http://jsonpath.com/" rel="nofollow noreferrer">this tool</a> against that JSON and works correctly, but when running the test I'm getting this assertion error:</p>

<pre><code>java.lang.AssertionError: JSON path "$._embedded.content[?(@.type=='JOB')].functions"
Expected: a collection with size &lt;3&gt;
    but: collection size was &lt;1&gt;
        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)
        ...
</code></pre>