org.mockito.stubbing
T
- the type to return.public interface Answer<T>
Example of stubbing a mock with custom answer:
when(mock.someMethod(anyString())).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + Arrays.toString(args);
}
});
//Following prints "called with arguments: [foo]"
System.out.println(mock.someMethod("foo"));
Modifier and Type | Method and Description |
---|---|
T |
answer(InvocationOnMock invocation) |
T answer(InvocationOnMock invocation) throws Throwable
invocation
- the invocation on the mock.Throwable
- the throwable to be thrown