Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading