title: SpringMVC/ mockMVC/ jsonpath compare list of strings
body: <p>I am currently writing some unit tests for a Spring MVC project.
  As the returned media type is JSON, I try to use jsonPath to check if the correct values are returned.</p>
  
  <p>The trouble I have is to verify if a list of strings contains the correct (and only the correct) values.</p>
  
  <p>My Plan was:</p>
  
  <ol>
  <li>Check that the list has the correct length</li>
  <li>For each element that's supposed to be returned, check whether it's in the list</li>
  </ol>
  
  <p>sadly, none of these things seem to work.</p>
  
  <p>Here's the relevant part of my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>Collection&lt;AuthorityRole&gt; correctRoles = magicDataSource.getRoles();
  
  ResultActions actions = this.mockMvc.perform(get("/accounts/current/roles").accept(MediaType.APPLICATION_JSON))
  .andExpect(status().isOk()) // works
  .andExpect(jsonPath("$.data.roles").isArray()) // works
  .andExpect(jsonPath("$.data.roles.length").value(correctRoles.size())); // doesn't work
  
  for (AuthorityRole role : correctRoles) // doesn't work
    actions.andExpect(jsonPath("$.data.roles[?(@=='%s')]", role.toString()).exists());
  </code></pre>
  
  <p>Only the first two "expectations" (isOk &amp; isArray) are working. The other ones (for length and content) I can twist and turn however I want, they're not giving me any useful result.</p>
  
  <p>Any suggestions?</p>