0

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.

np_
  • 1,435
  • 1
  • 7
  • 11
  • Does this answer your question? [How to negate an ArgumentMatcher in Mockito?](https://stackoverflow.com/questions/40215935/how-to-negate-an-argumentmatcher-in-mockito) – Akshay G Oct 18 '22 at 12:33
  • @AkshayG Using `Mockito.argThat` results in a compilation error – np_ Oct 18 '22 at 13:37

1 Answers1

1

You're close.

import static org.hamcrest.Matchers.not;
... 
.andExpect(jsonPath("$.errorMessage").value(not(description)));
ALex
  • 673
  • 1
  • 6
  • 19
  • Thank you. For some reason intellij was not recommending hamcrest at all, even though it is on my classpath via spring boot. – np_ Nov 05 '22 at 18:04