How to verify method is called with specific arguments once, but ignore calls with other arguements?
I have a method that I am trying to verify is called once with specific arguments, but I don't care if the method is called any other number of times with different arguments. How can I do this in Mockito.
For example:
obj.method("example", example); // expected
obj.method("example1", example2); // indifferent
obj.method("example", example); // unexpected
verify(obj).method("example", example); // will pass
asked Mar 17, 2016 at 14:57
Nelson
3,3023 gold badges27 silver badges37 bronze badges
1 Answer 1
verify(obj).method("example", example); // will pass
The indifferent code will not cause verify to fail.
My issue was that the expected line was not being executed and the indifferent showed up as different from what was expected after the unit test ran. Causing me to believe the expected line ran and the indifferent line to cause the failure.
answered Mar 17, 2016 at 17:25
Nelson
3,3023 gold badges27 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java
example2to have any influence on your verify there, unlessexample.equals(example2). For example, assumex.doSomething("a"); x.doSomething("b");->Mockito.verify(x).doSomething("a");- works, while if we addedx.doSomething(new String("a");it would not work.