This question already has an answer here:
How can I write a matcher using Mockito that matches any string except a specific one?
I have tried using some hamcrest matchers to negate and combine other matchers, but the hamcrest matchers all return values of type Matcher<T>
which dont work very well with Mockito matchers.
The solution I used:
argThat(not("ExceptionString"))
Where argThat
is a Mockito matcher,
and not
is a Hamcrest Matcher
Just point that with Mockito
you can also use AdditionalMatchers and ArgumentMatchers
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.ArgumentMatchers.eq;
//anything but not "ejb"
mock.someMethod(not(eq("ejb")));
According its documentation:
Example of using logical and(), not(), or() matchers:
//anything but not "ejb"
mock.someMethod(not(eq("ejb")));
There is more info in this other SO question
Hope it helps
来源:https://stackoverflow.com/questions/30107476/mockito-match-any-string-except-one