I'm looking for a way to make a mock function return different values based on the generic type of its parameter. E.g, if the parameter is any instance of Generic<Foo> do something, and if the parameter is any instance of Generic<Bar> do something else.
I thought I'll be able to acomplish that using the following code:
@Mock
private Getter getter;
@Rule
public final Mocks mocks = new Mocks(this);
@Test
public void testSearch(){
doReturn("Foo<Boolean>").when(getter).getValue(Matchers.<Foo<Boolean>>any());
doReturn("Foo<String>").when(getter).getValue(Matchers.<Foo<String>>any());
System.out.println(getter.getValue(new Foo<Boolean>()));
System.out.println(getter.getValue(new Foo<String>()));
}
public static class Foo<T> {
}
public class Getter {
<T> String getValue(BumapinRefOdsHandlerTest.Foo<T> foo) {
return "not-used";
}
}
However the code prints
Foo<String>
Foo<String>
What is the correct way to match parameters base on generic type?