I am having a problem with mocking a method with mockito that looks like the following:
Map<Foo, ? extends Collection<Bar>> getValue();
The following is how I am using it in the test:
model = Mockito.mock(Model.class);
Map<Foo, List<Bar>> value = new HashMap<Foo, List<Bar>>();
Mockito.when(model.getValue()).thenReturn(value);
It gives the following error:
error: no suitable method found for
thenReturn(Map<Foo,List<Bar>>)
You may use the following:
model = Mockito.mock(Model.class);
final Map<Foo, List<Bar>> value = new HashMap<Foo, List<Bar>>();
Mockito.when(model.getValue()).thenAnswer(new Answer<Map<Foo, List<Bar>>> () {
public Map<Foo, List<Bar>> answer(InvocationOnMock invocation) throws Throwable {
return value;
}
});
Above can be shortened using lambda as:
Mockito.when(model.getValue()).thenAnswer(invocationOnMock -> value)
This error is happening because the compiler can't guarantee that the value type of the map returned by getValue
is in fact List<Bar>
. The type Map<Foo, ? extends Collection>
means "a Map
of Foo
s to some unknown type implementing Collection
".
This is a good example of why using wildcards in return types is discouraged, because they generally inhibit the caller by obscuring the generic type information about what's returned (conversely, using wildcards in method parameters is encouraged because it makes things easier for the caller). I would recommend getting rid of the wildcard if possible:
Map<Foo, Collection<Bar>> getValue();
And use:
model = Mockito.mock(Model.class);
Map<Foo, Collection<Bar>> value = new HashMap<Foo, Collection<Bar>>();
Mockito.when(model.getValue()).thenReturn(value);
If you're unable to change the method's return type, you could use a "capture helper" method for the test:
private <T extends Collection<Bar>> test(Map<Foo, T> actual) {
Map<Foo, T> expected = new HashMap<Foo, T>();
Mockito.when(actual).thenReturn(expected);
}
...
model = Mockito.mock(Model.class);
test(model.getValue()); // T is resolved to wildcard capture
Of course this is very limiting because you can only test for an empty map without knowing what T
is.
If you don't want to write helper functions, using doReturn
...when
works too, like this (although it's not type-safe):
Mockito.doReturn(value).when(model).getValue();
public class Test {
interface Model {
Map<Foo, ? extends Collection<Bar>> getValue();
}
class Bar {}
class Foo {}
public static void main(String[] args) {
Model model = Mockito.mock(Model.class);
Map<Foo, List<Bar>> value = new HashMap<Foo, List<Bar>>();
// when(model.getValue()).thenReturn(value); // won't compile
doReturn(value).when(model).getValue();
System.out.println(model.getValue());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With