Mockito: Match any String except one

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

  • Mockito 3.3.3
  • Hamcrest 1.3