EDIT : I finally created the issue mickito github project.
I'm trying to make fun of the typed getNameElement method of the getNameElement interface to return the first arg using the Mockito AdditionalAnswers.returnsFirstArg functionality:
Interface for layout :
interface PrimaryKeyElement<T> { public String getNameElement(T primaryKey); } interface RoomGeneralService extends PrimaryKeyElement<String> {
My test (pay attention to import)
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());
I also tried with other combinations, but without success:
when(roomGeneralService.getNameElement(Matchers.<String>any())).thenAnswer(returnsFirstArg()); doAnswer(returnsFirstArg()).when(roomGeneralService.getNameElement(anyString())); doReturn(returnsFirstArg()).when(roomGeneralService.getNameElement(anyString()));
Received error :
The cause of this error may be: 1. The desired position of the argument is incorrect. 2. The answer is used for incorrect interaction.
The position of the desired argument is 0, and the possible argument arguments for this method are: [0] Object
Bypass
I know that I can create my own answer, and actually it works fine if, instead of using 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 my other tests (the tests look cleaner), and the mockery works fine if the getNameElement method gets a String instead of a T arg.
Thanks for the help.