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.
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
The solution I used:
import static org.hamcrest.CoreMatchers.not;
import static org.mockito.ArgumentMatchers.argThat;
// ...
argThat(not("ExceptionString"))
Versions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With