简体   繁体   中英

Mockito verify skip a number of calls?

I want to check the parameter passed to a method ... but the check has to be done on the 3rd time this method gets called.

I thought the answer might be to go:

verify( myMock, times( 2 ) ).myMethod( any() );
verify( myMock ).myMethod( paramIWant ); 

... but it fails on the first line:

But was 5 times. Undesired invocation:

... because there are indeed 2 more calls after the one I'm interested in.

Use an argument captor for this. You can pass the captor in when you verify, then get out all the values that were passed as a parameter to this method and check whichever you want.

ArgumentCaptor<SomeClass> myCaptor = ArgumentCaptor.forClass(SomeClass.class);
verify(myMock,times(5)).myMethod(myCaptor.capture());

List<SomeClass> paramsPassed = myCaptor.getAllValues();
assertEquals(paramIWant, paramsPassed.get(2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM