I have a service class with the following method:
void doSomething(List<String> list)
I mock this class and I want to verify that a list which is passed as a parameter has only one element. I do it like this:
verify(myService).doSomething((List<String>) argThat(hasSize(1))))
As you see I have to cast the argument to List<String>
otherwise it doesn't get compiled:
incompatible types: inferred type does not conform to upper bound(s)
inferred: java.util.Collection<? extends java.lang.Object>
upper bound(s): java.util.List<java.lang.String>,java.lang.Object
Question: How can I verify the call without casting? I want to keep things simple, readable and elegant.
I prefer this solution:
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(myService).doSomething(argumentCaptor.capture());
assertThat(argumentCaptor.getValue().size()).isEqualTo(1);