There are two approaches to this:
- simply casting on
any()
acts as a sufficient type hint:
Mockito.doReturn("1")
.when(classMock)
.name(eq(1), (List<List<String>>) any());
- providing additional generics information to
anyList()
.
This is basically the same approach as below answer, only a little more concise as the alias Mockito
is used, which maps to the ArgumentMatcher
implementation.
Mockito.doReturn("1")
.when(classMock)
.name(eq(1), Mockito.<List<String>> anyList());
There are subtle but notable differences between the two. Imagine name()
had the following two overloads:
// Overload A (target of this test)
String name(int id, Object entities) {...}
// Overload B (not targeted in this test)
String name(int id, List<CustomMapImpl> entities) {...}
When the second argument becomes null
in the code under test, approach 1. will also properly match overload A, while approach 2. will resolve to overload B. In order match that specific case with generics it would need to be explicitly defined using a different ArgumentMatcher:
Mockito.doReturn(null)
.when(classMock)
.name(eq(1), Mockito.<List<List<String>>>isNull());