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));