I'm using mockito to write some tests, and I'm using the following bit of code:
ArgumentCaptor<LinkedList> captor = ArgumentCaptor.forClass(LinkedList.class);
This compiles and runs just fine, except for the warning that "captor" is raw type and I should replace it with something like:
ArgumentCaptor<LinkedList<String>> captor = ArgumentCaptor.forClass(LinkedList<String>.class);
The problem is that LinkedList<String>.class doesn't exist, so the right side of the assignment will never compile.
Assuming that suppressing the warning is inelegant, is there an elegant solution? If not, why does the compile warn me about something I can't really fix?
There is a
@Captor
annotation in Mockito which is designed to allow you to avoid warnings like this.Using it, you don't need to manually
new
up yourArgumentCaptor
. You can just declare it with the annotation and then callMockitoAnnotations.initMocks(this);
to "automatically" create your captor.instead of this:
do this: