Search code examples
javamockitohamcrest

Is there a `not` ArgumentMatcher for mockito stubbing


I am trying to verify that a method is called with a long having any value but a given one.

I thus would like to know if there is an ArgumentMatcher that fits to my use case, such as:

verify(mObject).verifiedMethod(notEq(longValueThatShouldBeAvoided));

I found this workaround:

verify(mObject).method(longThat(arg -> arg != longValueThatShouldBeAvoided));

But I find weird that such simple ArgumentMatcher has to be written from scratch.


Additional question: How to proceed when checking for multiple values to avoid ?

Similarly, I found the workaround of using arg -> arg != val0 && arg != val1 lambda as parameter of ArgumentsMatcher.longThat method to achieve this.


Solution

  • try:

    import static org.mockito.AdditionalMatchers.not; 
    import static org.mockito.ArgumentMatchers.eq;
    
    verify(mObject).verifiedMethod(not(eq(longValueThatShouldBeAvoided)));