Why is the or-Matcher not working in my Mockito verify?

I would like to verify that either of the following two method calls get executed once:

myLogger.logWarn("My 1st Warning Message"); // depending on some randomness in my program,
myLogger.logWarn("My 2nd Warning Message"); // one of these two get executed

I have tried the following:

verify(myLogger).logWarn(or("My 1st Warning Message", "My 2nd Log Warning Message"));

But running the test method resulted in the following error:

No matchers found for additional matcher Or(?)
-> at foobar.builder.StopBuilderTest.build(StopBuilderTest.java:141)

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
No matchers found for additional matcher Or(?)
-> at foobar.builder.StopBuilderTest.build(StopBuilderTest.java:141)

When I just test for a single method call as follows..

verify(myLogger).logWarn("My 1st Warning Message");

..it runs fine and my test is always successful when logWarn() gets called with argument "My 1st Warning Message".

>Solution :

AdditionalMatchers are used to implement common logical operations (‘not’, ‘and’, ‘or’) on ArgumentMatchers

So following code:

 verify(myLogger).logWarn(or(eq("My 1st Warning Message"), eq("My 2nd Log Warning Message")));

Should work as expected.

Leave a Reply