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.
1) Instead of
.andExpect(jsonPath("$.data.roles.length").value(correctRoles.size()));
try
.andExpect(jsonPath("$.data.roles.length()").value(correctRoles.size()));
or
.andExpect((jsonPath("$.data.roles", Matchers.hasSize(size))));
2) Instead of
for (AuthorityRole role : correctRoles) // doesn't work
actions.andExpect(jsonPath("$.data.roles[?(@=='%s')]", role.toString()).exists());
try
actions.andExpect((jsonPath("$.data.roles", Matchers.containsInAnyOrder("role1", "role2", "role3"))));
Keep in mind that you have to add hamcrest-library.