Interface Answer<T>

Type Parameters:
T - the type to return.
All Known Subinterfaces:
Stubbing
All Known Implementing Classes:
AbstractThrowsException, Answers, AnswersWithDelay, CallsRealMethods, ClonesArguments, DoesNothing, ForwardsInvocations, GloballyConfiguredAnswer, Returns, ReturnsArgumentAt, ReturnsDeepStubs, ReturnsDeepStubs.DeeplyStubbedAnswer, ReturnsDeepStubs.ReturnsDeepStubsSerializationFallback, ReturnsElementsOf, ReturnsEmptyValues, ReturnsMocks, ReturnsMoreEmptyValues, ReturnsSmartNulls, ReturnsSmartNulls.ThrowsSmartNullPointer, StubbedInvocationMatcher, ThrowsException, ThrowsExceptionForClassType, TriesToReturnSelf

public interface Answer<T>
Generic interface to be used for configuring mock's answer. Answer specifies an action that is executed and a return value that is returned when you interact with the mock.

Example of stubbing a mock with custom answer:

when(mock.someMethod(anyString())).thenAnswer(
    new Answer() {
        public 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"));
  • Method Summary

    Modifier and Type
    Method
    Description
     
  • Method Details

    • answer

      T answer(InvocationOnMock invocation) throws Throwable
      Parameters:
      invocation - the invocation on the mock.
      Returns:
      the value to be returned
      Throws:
      Throwable - the throwable to be thrown