Making a Mocked Method Return an Argument That Was Passed to It

Making a mocked method return an argument that was passed to it

Since Mockito 1.9.5+ and Java 8+

You can use a lambda expression, like:

when(myMock.myFunction(anyString())).thenAnswer(i -> i.getArguments()[0]);

Where i is an instance of InvocationOnMock.

For older versions

You can create an Answer in Mockito. Let's assume, we have an interface named MyInterface with a method myFunction.

public interface MyInterface {
public String myFunction(String abc);
}

Here is the test method with a Mockito answer:

public void testMyFunction() throws Exception {
MyInterface mock = mock(MyInterface.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"));
}

How to return the argument passed to a Mockito-mocked method in the thenReturn function?

You do this usually with thenAnswer:

when(doSomething.perform(firstParameter, any(Integer.class), 
any(File.class)))
.thenAnswer(new Answer<File>() {
public File answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgument(0);
}
};

and you can simplify this to be

when(doSomething.perform(firstParameter, any(Integer.class), any(File.class)))
.thenAnswer(invocation -> invocation.getArgument(0));

How to return the argument, which the mock method was called with, as response?

The following should work:

when(myObjectRepository.save(any(MyObject.class)))
.then(AdditionalAnswers.returnsFirstArg());

Return argument of mocked method as a result

You can write your own Answer:

when(mock.testMethod(anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) {
return invocation.getArgumentAt(0, String.class);
}
});

Or:

when(mock.testMethod(anyString()))
.thenAnswer(AdditionalAnswers.<String>returnsFirstArg());

Mockito - returning the same object as passed into method

You can implement an Answer and then use thenAnswer() instead.

Something similar to:

when(mock.someMethod(anyString())).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
return invocation.getArguments()[0];
}
});

Of course, once you have this you can refactor the answer into a reusable answer called ReturnFirstArgument or similar.

Mock a method and getting in return a parameter of the passed class

You can use ArgumentCaptor to capture the argument and return the value from it.

final ArgumentCaptor<THAT> thatCaptor = ArgumentCaptor.forClass(THAT.class);

doAnswer (invocation -> {
return thatCaptor.getValue().getAVar()//use the variable directly if its public
}).when(This).METH(thatCaptor.capture());

JAVA : JUNIT Argument passed to when() is not a mock

Here:

when(testAPIKeyAuthHandler)

But:

@InjectMocks
private APIKeyAuthHandler testAPIKeyAuthHandler;

testAPIKeyAuthHandler is not a mock, and the syntax for doing a when() specification will not change that.

Meaning: the @InjectMocks annotation does not create a mock. It creates an object that Mockito will try to populate with your mock objects!

But note, the real answer here: these things are complicated. You can't learn how to use mockito in conjunction with a complex framework like spring by trial and error. Thus: you better step back. Pick up a good tutorial on how to use mockito. Read that, and understand how to use Mockito the proper way. Then pick up a tutorial that explains Mockito in conjunction with spring, read that, understand it. And then apply what you have learned to your setup.

Mocking return with any passed argument

Try to use any() from Mockito framework instead of (new FileReader("pom.xml"))

For example:

import static org.mockito.ArgumentMatchers.any;

...
when(mockReader.read(any(Reader.class)).thenReturn(mockModel);
...


Related Topics



Leave a reply



Submit