3

I have a problem using captors to test two separate calls of the same method, but with different argument type.

I provided a code sample of what I'm trying to do here - basically I want to verify if certain method was called twice but with different type arguments using two separate captors, and then call some assertions on captured objects.

public class Foo { } public class Bar { } public interface MyClient { void doSomething(Object obj); } public class MyService { private MyClient client; void doSomething() { client.doSomething(new Foo()); client.doSomething(new Bar()); } } @RunWith(MockitoJUnitRunner.class) public class MyServiceTest { @InjectMocks private MyService testObj; @Mock private MyClient myClient; @Captor private ArgumentCaptor<Foo> fooCaptor; @Captor private ArgumentCaptor<Bar> barCaptor; @Test public void testSomething() { testObj.doSomething(); verify(myClient).doSomething(fooCaptor.capture()); // ...do something with fooCaptor verify(myClient).doSomething(barCaptor.capture()); // ...do something with barCaptor } } 

I'd expect this test would pass as is, because captors specify argument types, so shouldn't this work same as ArgumentMatchers.any(Foo.class) etc?

Currently I'm getting TooManyActualInvocations - 2 instead of 1.

How do we handle such cases? I don't like the idea of using one captor and then cast the results.

0

1 Answer 1

1

Try to combine the captors with the isA matcher using the and operator from the AdditionalMatchers package.

import static org.mockito.AdditionalMatchers.and; // ... verify(myClient).doSomething(and(isA(Foo.class), fooCaptor)); verify(myClient).doSomething(and(isA(Bar.class), barCaptor)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Captor is not a matcher, I guess you meant verify(myClient).doSomething(and(isA(Foo.class), fooCaptor.capture())) etc, but still Mockito is unable to handle this situation when you try to call getValue: org.mockito.exceptions.base.MockitoException: No argument value was captured! You might have forgotten to use argument.capture() in verify()...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.