Mockito: match any line except one

How can I write a match using Mockito that matches any string except a specific one?

I tried using some hamcrest helpers to negate and merge other matches, but hamcrest matches return values ​​like Matcher<T> , which don't work very well with Mockito layouts.

+8
source share
2 answers

The solution I used:

 argThat(not("ExceptionString")) 

Where argThat is the Mockito binder,
and not is a Hamcrest match

+12
source

Just indicate that with Mockito you can also use additional Mockito and arguments

 import static org.mockito.AdditionalMatchers.not; import static org.mockito.ArgumentMatchers.eq; //anything but not "ejb" mock.someMethod(not(eq("ejb"))); 

According to his documentation:

An example of using logical and (), not () or () matches:

// nothing but ejb
mock.someMethod (not (equiv ("EJB")));

There is more info on this other SO question

Hope help

+9
source

All Articles