Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito.verify method contain boolean and argument captor

Tags:

java

mockito

I don't know how can I use Mockito.verify in this case. How can I pass false to Mockito.verify? I try 2 different ways but it does not work.

public Sample someMethod(Sample s, boolean a){....}
@Test
public void test() {
...
verify(mock).someMethod(sampleCaptor.capture(), false));
verify(mock).someMethod(sampleCaptor.capture(), org.mockito.Matchers.eq(false)));
...
}
like image 886
Thanh Duy Ngo Avatar asked Oct 29 '15 10:10

Thanh Duy Ngo


People also ask

What does verify method do in Mockito?

Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.

What does captor do in Mockito?

Using ArgumentCaptor. ArgumentCaptor allows us to capture an argument passed to a method to inspect it. This is especially useful when we can't access the argument outside of the method we'd like to test.

Does Mockito verify use equals?

Internally Mockito uses Point class's equals() method to compare object that has been passed to the method as an argument with object configured as expected in verify() method. If equals() is not overridden then java. lang.


1 Answers

You have it right the second way:

verify(mock).someMethod(sampleCaptor.capture(), Matchers.eq(false));

When using Matchers (including ArgumentCaptor.capture), you have to use a Matcher for every value, because Matchers work via side-effects.

If the above doesn't work, you may be misusing matchers earlier in the method. It is sometimes helpful to explicitly call Mockito.validateMockitoUsage() immediately before your call to verify, to ensure that there's nothing wrong with Mockito's internal state. (Additional information about how it "does not work", including a minimal reproducible example, may be helpful in solving your specific case.)

like image 58
Jeff Bowman Avatar answered Sep 19 '22 12:09

Jeff Bowman