T
- the type to return.AnswerReturnValuesAdapter
, CallsRealMethods
, ClonesArguments
, DoesNothing
, ForwardsInvocations
, GloballyConfiguredAnswer
, Returns
, ReturnsArgumentAt
, ReturnsDeepStubs
, ReturnsElementsOf
, ReturnsElementsOf
, ReturnsEmptyValues
, ReturnsMocks
, ReturnsMoreEmptyValues
, ReturnsSmartNulls
, StubbedInvocationMatcher
, ThrowsException
, ThrowsExceptionClass
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 | Description |
---|---|---|
T |
answer(InvocationOnMock invocation) |
T answer(InvocationOnMock invocation) throws java.lang.Throwable
invocation
- the invocation on the mock.java.lang.Throwable
- the throwable to be thrown