Home > other >  How do I test for inequality with Spring's MockMvc and jsonpath?
How do I test for inequality with Spring's MockMvc and jsonpath?

Time:11-06

I am trying to test that a field in the response is not equal to some value, like such:

@Test
void whenAnAuth0ExceptionIsThrown_itShouldNotLeakInformation() throws Exception {
    String description = "Resource not found.";
    Auth0Exception auth0Exception = new Auth0Exception(description);

    when(usersService.getRoles()).thenThrow(new UnrecoverableAuthException("api exception", auth0Exception));

    mockMvc.perform(get("/v1/users/roles").with(csrf()))
        .andExpect(status().isInternalServerError())
        .andExpect(jsonPath("$.errorMessage").value(AdditionalMatchers.not(description)));


    verify(usersService).getRoles();
}

But when I try this, I get the following error:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
No matchers found for additional matcher Not(?)

A workaround that I've found is to use andReturn() and then test the MvcResult.

CodePudding user response:

You're close.

import static org.hamcrest.Matchers.not;
... 
.andExpect(jsonPath("$.errorMessage").value(not(description)));
  • Related