Making a mocked method return an argument that was passed to it
You can create an Answer in Mockito. Let's assume, we have an interface named Application with a method myFunction.
public interface Application { public String myFunction(String abc);}
Here is the test method with a Mockito answer:
public void testMyFunction() throws Exception { Application mock = mock(Application.class); when(mock.myFunction(anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); return (String) args[0]; } }); assertEquals("someString",mock.myFunction("someString")); assertEquals("anotherString",mock.myFunction("anotherString"));}
Since Mockito 1.9.5 and Java 8, you can also use a lambda expression:
when(myMock.myFunction(anyString())).thenAnswer(i -> i.getArguments()[0]);
If you have Mockito 1.9.5 or higher, there is a new static method that can make the Answer
object for you. You need to write something like
import static org.mockito.Mockito.when;import static org.mockito.AdditionalAnswers.returnsFirstArg;when(myMock.myFunction(anyString())).then(returnsFirstArg());
or alternatively
doAnswer(returnsFirstArg()).when(myMock).myFunction(anyString());
Note that the returnsFirstArg()
method is static in the AdditionalAnswers
class, which is new to Mockito 1.9.5; so you'll need the right static import.
With Java 8 it is possible to create a one-line answer even with older version of Mockito:
when(myMock.myFunction(anyString()).then(i -> i.getArgumentAt(0, String.class));
Of course this is not as useful as using AdditionalAnswers
suggested by David Wallace, but might be useful if you want to transform argument "on the fly".