Alexei Blue Alexei Blue - 4 months ago 38
Java Question

Mockito ArgumentCapture to doesn't recognise multiple generic types

I'm trying to use Mockito's ArgumentCapture to retrieve a generic argument used however in my method the same base type is used but with different generic arguments twice.

To simplify the example I've written a test different from my code but with the same problem:

@Captor private ArgumentCaptor<ArrayList<String>> stringl;
@Captor private ArgumentCaptor<ArrayList<Boolean>> booleanl;

public void setup()
} //setup

public void test()
Foo foo = mock(Foo.class);

List<String> stringList = new ArrayList<String>();
List<Boolean> booleanList = new ArrayList<Boolean>();


} //test

private static class Foo
public <T> void doSomething(List<T> list){}

Executing the test produces the following error:

foo.doSomething(<Capturing argument>);
Wanted 1 time:
-> at test(
But was 2 times. Undesired invocation:
-> at test(

To see what was happening I added
to the verify methods and then inspected the argument captures. Both picked up the second invocation meaning I could never capture the first argument of type

Is there a way to get ArgumentCapture to recognise different generic types for the same base type i.e. differentiate between List and List?

Alexei Blue


Not using the existing ArgumentCaptor class. Due to type-erasure this information is lost. I suggest you use a single captor and get the argument passed in all invocations. You can then verify that it was called the first time with List<String> and the second time with List<Boolean>. Of course you would do this by verifying the contents of the Lists.