Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn\'t work:
ArgumentCaptor> argumen
Yeah, this is a general generics problem, not mockito-specific.
There is no class object for ArrayList
, and thus you can't type-safely pass such an object to a method requiring a Class
.
You can cast the object to the right type:
Class> listClass =
(Class>)(Class)ArrayList.class;
ArgumentCaptor> argument = ArgumentCaptor.forClass(listClass);
This will give some warnings about unsafe casts, and of course your ArgumentCaptor can't really differentiate between ArrayList
and ArrayList
without maybe inspecting the elements.
(As mentioned in the other answer, while this is a general generics problem, there is a Mockito-specific solution for the type-safety problem with the @Captor
annotation. It still can't distinguish between an ArrayList
and an ArrayList
.)
Take also a look at tenshis comment. You can change the original code from Paŭlo Ebermann to this (much simpler)
final ArgumentCaptor> listCaptor
= ArgumentCaptor.forClass((Class) List.class);