EDIT: I've finally created an issue on mockito github project.
I'm trying to mock the typed method getNameElement
of Interface RoomGeneralService
to return the first arg, using Mockito AdditionalAnswers.returnsFirstArg
functionality:
Interface to mock:
interface PrimaryKeyElement<T> {
public String getNameElement(T primaryKey);
}
interface RoomGeneralService extends PrimaryKeyElement<String> {
// ...
}
My test (note the imports)
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
public class SampleTest {
@Mock
RoomGeneralService roomGeneralService;
@Test
public void testFoo() throws Exception {
when(roomGeneralService.getNameElement(anyString())).thenAnswer(returnsFirstArg());
//...
}
}
Also I've tried with other combinations, but without success so far:
when(roomGeneralService.getNameElement(Matchers.<String>any())).thenAnswer(returnsFirstArg());
doAnswer(returnsFirstArg()).when(roomGeneralService.getNameElement(anyString()));
doReturn(returnsFirstArg()).when(roomGeneralService.getNameElement(anyString()));
Error received:
The reason for this error can be : 1. The wanted argument position is incorrect. 2. The answer is used on the wrong interaction.
Position of the wanted argument is 0 and the possible argument indexes for this method are : [0] Object
Workaround:
I know I can create my own answer, and in fact it's working fine if instead of use returnFirstArg()
I do something like this:
when(roomGeneralService.getNameElement(anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return (String) invocation.getArguments()[0];
}
});
But I would use returnFirstArg()
as in the rest of my tests (tests look cleaner), as well as mocking is working fine if the method getNameElement
would receive an String
instead a T
arg.
Thanks for the help.
It seems Mockito isn't smart enough to deduce that the parameter type will be bound to String
in the parameterized subinterface.
You can override the method in the subinterface
interface RoomGeneralService extends PrimaryKeyElement<String> {
@Override
public String getNameElement(String primaryKey);
}
Mockito won't have to guess. It will clearly see String
as the parameter type of the stubbed method.
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