Mockito: Match any String except one

Stephan picture Stephan · May 7, 2015 · Viewed 15.4k times · Source

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.

Answer

Stephan picture Stephan · May 7, 2015

The solution I used:

import static org.hamcrest.CoreMatchers.not;
import static org.mockito.ArgumentMatchers.argThat;

// ...

argThat(not("ExceptionString"))

Versions