Package org.mockito

Class AdditionalAnswers

java.lang.Object
org.mockito.AdditionalAnswers

public final class AdditionalAnswers extends Object
Additional answers provides factory methods for answers.

Currently offer answers that can return the parameter of an invocation at a certain position, along with answers that draw on a strongly typed interface to provide a neater way to write custom answers that either return a value or are void (see answer interfaces in org.mockito.stubbing).

See factory methods for more information : returnsFirstArg(), returnsSecondArg(), returnsLastArg(), returnsArgAt(int), answer(org.mockito.stubbing.Answer1<T, A>) and answerVoid(org.mockito.stubbing.VoidAnswer1<A>)

Since:
1.9.5
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, A> Answer<T>
    answer(Answer1<T,A> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <T, A, B> Answer<T>
    answer(Answer2<T,A,B> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <T, A, B, C>
    Answer<T>
    answer(Answer3<T,A,B,C> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <T, A, B, C, D>
    Answer<T>
    answer(Answer4<T,A,B,C,D> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <T, A, B, C, D, E>
    Answer<T>
    answer(Answer5<T,A,B,C,D,E> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <T, A, B, C, D, E, F>
    Answer<T>
    answer(Answer6<T,A,B,C,D,E,F> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created idiomatically in Java 8
    static <T> Answer<T>
    answersWithDelay(long sleepyTime, Answer<T> answer)
    Returns an answer after a delay with a defined length.
    static <A> Answer<Void>
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <A, B> Answer<Void>
    answerVoid(VoidAnswer2<A,B> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <A, B, C> Answer<Void>
    answerVoid(VoidAnswer3<A,B,C> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <A, B, C, D>
    Answer<Void>
    answerVoid(VoidAnswer4<A,B,C,D> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <A, B, C, D, E>
    Answer<Void>
    answerVoid(VoidAnswer5<A,B,C,D,E> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
    static <A, B, C, D, E, F>
    Answer<Void>
    answerVoid(VoidAnswer6<A,B,C,D,E,F> answer)
    Creates an answer from a functional interface - allows for a strongly typed answer to be created idiomatically in Java 8
    static <T> Answer<T>
    delegatesTo(Object delegate)
    An answer that directly forwards the calls to the delegate.
    static <T> Answer<T>
    returnsArgAt(int position)
    Returns the parameter of an invocation at the given position.
    static <T> Answer<T>
    Returns elements of the collection.
    static <T> Answer<T>
    Returns the first parameter of an invocation.
    static <T> Answer<T>
    Returns the last parameter of an invocation.
    static <T> Answer<T>
    Returns the second parameter of an invocation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • returnsFirstArg

      public static <T> Answer<T> returnsFirstArg()
      Returns the first parameter of an invocation.

      This additional answer could be used at stub time using the then|do|willAnswer methods. For example :

      
       given(carKeyFob.authenticate(carKey)).will(returnsFirstArg());
       doAnswer(returnsFirstArg()).when(carKeyFob).authenticate(carKey);
       

      This methods works with varargs as well, mockito will expand the vararg to return the argument at the given position. Suppose the following signature :

      
       interface Person {
           Dream remember(Dream... dreams);
       }
      
       // returns dream1
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsFirstArg());
       
      Mockito will return the vararg array if the first argument is a vararg in the method and if the return type has the same type as the vararg array.
      
       interface Person {
           Dream[] remember(Dream... otherDreams);
       }
      
       // returns otherDreams (happens to be a 4 elements array)
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsFirstArg());
       

      Type Parameters:
      T - Return type of the invocation.
      Returns:
      Answer that will return the first argument of the invocation.
      Since:
      1.9.5
    • returnsSecondArg

      public static <T> Answer<T> returnsSecondArg()
      Returns the second parameter of an invocation.

      This additional answer could be used at stub time using the then|do|willAnswer methods. For example :

      
       given(trader.apply(leesFormula, onCreditDefaultSwap)).will(returnsSecondArg());
       doAnswer(returnsSecondArg()).when(trader).apply(leesFormula, onCreditDefaultSwap);
       

      This methods works with varargs as well, mockito will expand the vararg to return the argument at the given position. Suppose the following signature :

      
       interface Person {
           Dream remember(Dream dream, Dream... otherDreams);
       }
      
       // returns dream2
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsSecondArg());
       
      Mockito will return the vararg array if the second argument is a vararg in the method and if the return type has the same type as the vararg array.
      
       interface Person {
           Dream[] remember(Dream dream1, Dream... otherDreams);
       }
      
       // returns otherDreams (happens to be a 3 elements array)
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsSecondArg());
       

      Type Parameters:
      T - Return type of the invocation.
      Returns:
      Answer that will return the second argument of the invocation.
      Since:
      1.9.5
    • returnsLastArg

      public static <T> Answer<T> returnsLastArg()
      Returns the last parameter of an invocation.

      This additional answer could be used at stub time using the then|do|willAnswer methods. For example :

      
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsLastArg());
       doAnswer(returnsLastArg()).when(person).remember(dream1, dream2, dream3, dream4);
       

      This methods works with varargs as well, mockito will expand the vararg to return the argument at the given position. Suppose the following signature :

      
       interface Person {
           Dream remember(Dream dream, Dream... otherDreams);
       }
      
       // returns dream4
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsLastArg());
       
      Mockito will return the vararg array if the given position targets the vararg index in the method and if the return type has the same type as the vararg array.
      
       interface Person {
           Dream[] remember(Dream dream1, Dream dream2, Dream dream3, Dream... otherDreams);
       }
      
       // returns otherDreams (happens to be a single element array)
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsLastArg());
       

      Type Parameters:
      T - Return type of the invocation.
      Returns:
      Answer that will return the last argument of the invocation.
      Since:
      1.9.5
    • returnsArgAt

      public static <T> Answer<T> returnsArgAt(int position)
      Returns the parameter of an invocation at the given position.

      This additional answer could be used at stub time using the then|do|willAnswer methods. For example :

      
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsArgAt(3));
       doAnswer(returnsArgAt(3)).when(person).remember(dream1, dream2, dream3, dream4);
       

      This methods works with varargs as well, mockito will expand the vararg to return the argument at the given position. Suppose the following signature :

      
       interface Person {
           Dream remember(Dream dream, Dream... otherDreams);
       }
      
       // returns dream 3
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsArgAt(2));
       
      Mockito will return the vararg array if the given position targets the vararg index in the method and if the return type has the same type as the vararg array.
      
       interface Person {
           Dream[] remember(Dream dream, Dream... otherDreams);
       }
      
       // returns otherDreams array (contains dream2, dream,3, dream4)
       given(person.remember(dream1, dream2, dream3, dream4)).will(returnsArgAt(1));
       

      Type Parameters:
      T - Return type of the invocation.
      Parameters:
      position - index of the argument from the list of arguments.
      Returns:
      Answer that will return the argument from the given position in the argument's list
      Since:
      1.9.5
    • delegatesTo

      public static <T> Answer<T> delegatesTo(Object delegate)
      An answer that directly forwards the calls to the delegate. The delegate may or may not be of the same type as the mock. If the type is different, a matching method needs to be found on delegate type otherwise an exception is thrown.

      Useful for spies or partial mocks of objects that are difficult to mock or spy using the usual spy API. Possible use cases:

      • Final classes but with an interface
      • Already custom proxied object
      • Special objects with a finalize method, i.e. to avoid executing it 2 times

      The difference with the regular spy:

      • The regular spy (Mockito.spy(Object)) contains all state from the spied instance and the methods are invoked on the spy. The spied instance is only used at mock creation to copy the state from. If you call a method on a regular spy and it internally calls other methods on this spy, those calls are remembered for verifications, and they can be effectively stubbed.
      • The mock that delegates simply delegates all methods to the delegate. The delegate is used all the time as methods are delegated onto it. If you call a method on a mock that delegates and it internally calls other methods on this mock, those calls are not remembered for verifications, stubbing does not have effect on them, too. Mock that delegates is less powerful than the regular spy but it is useful when the regular spy cannot be created.
      An example with a final class that we want to delegate to:

      
         final class DontYouDareToMockMe implements list { ... }
      
         DontYouDareToMockMe awesomeList = new DontYouDareToMockMe();
      
         List mock = mock(List.class, delegatesTo(awesomeList));
       

      This feature suffers from the same drawback as the spy. The mock will call the delegate if you use regular when().then() stubbing style. Since the real implementation is called this might have some side effects. Therefore, you should use the doReturn|Throw|Answer|CallRealMethod stubbing style. Example:

      
         List listWithDelegate = mock(List.class, AdditionalAnswers.delegatesTo(awesomeList));
      
         //Impossible: real method is called so listWithDelegate.get(0) throws IndexOutOfBoundsException (the list is yet empty)
         when(listWithDelegate.get(0)).thenReturn("foo");
      
         //You have to use doReturn() for stubbing
         doReturn("foo").when(listWithDelegate).get(0);
       
      Parameters:
      delegate - The delegate to forward calls to. It does not have to be of the same type as the mock (although it usually is). The only requirement is that the instance should have compatible method signatures including the return values. Only the methods that were actually executed on the mock need to be present on the delegate type.
      Returns:
      the answer
      Since:
      1.9.5
    • returnsElementsOf

      public static <T> Answer<T> returnsElementsOf(Collection<?> elements)
      Returns elements of the collection. Keeps returning the last element forever. Might be useful on occasion when you have a collection of elements to return.

      
         //this:
         when(mock.foo()).thenReturn(1, 2, 3);
      
         //is equivalent to:
         when(mock.foo()).thenAnswer(AdditionalAnswers.returnsElementsOf(Arrays.asList(1, 2, 3)));
       
      Parameters:
      elements - The collection of elements to return.
      Returns:
      the answer
      Since:
      1.9.5
    • answersWithDelay

      public static <T> Answer<T> answersWithDelay(long sleepyTime, Answer<T> answer)
      Returns an answer after a delay with a defined length.
      Type Parameters:
      T - return type
      Parameters:
      sleepyTime - the delay in milliseconds
      answer - interface to the answer which provides the intended return value.
      Returns:
      the answer object to use
      Since:
      2.8.44
    • answer

      public static <T, A> Answer<T> answer(Answer1<T,A> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answerVoid

      public static <A> Answer<Void> answerVoid(VoidAnswer1<A> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      A - input parameter type 1
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answer

      public static <T, A, B> Answer<T> answer(Answer2<T,A,B> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      B - input parameter type 2
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answerVoid

      public static <A, B> Answer<Void> answerVoid(VoidAnswer2<A,B> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      A - input parameter type 1
      B - input parameter type 2
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answer

      public static <T, A, B, C> Answer<T> answer(Answer3<T,A,B,C> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answerVoid

      public static <A, B, C> Answer<Void> answerVoid(VoidAnswer3<A,B,C> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answer

      public static <T, A, B, C, D> Answer<T> answer(Answer4<T,A,B,C,D> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answerVoid

      public static <A, B, C, D> Answer<Void> answerVoid(VoidAnswer4<A,B,C,D> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answer

      public static <T, A, B, C, D, E> Answer<T> answer(Answer5<T,A,B,C,D,E> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      E - input parameter type 5
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answerVoid

      public static <A, B, C, D, E> Answer<Void> answerVoid(VoidAnswer5<A,B,C,D,E> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created ideally in Java 8
      Type Parameters:
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      E - input parameter type 5
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.1.0
    • answer

      public static <T, A, B, C, D, E, F> Answer<T> answer(Answer6<T,A,B,C,D,E,F> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created idiomatically in Java 8
      Type Parameters:
      T - return type
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      E - input parameter type 5
      F - input parameter type 6
      Parameters:
      answer - interface to the answer - which is expected to return something
      Returns:
      the answer object to use
      Since:
      2.26.0
    • answerVoid

      public static <A, B, C, D, E, F> Answer<Void> answerVoid(VoidAnswer6<A,B,C,D,E,F> answer)
      Creates an answer from a functional interface - allows for a strongly typed answer to be created idiomatically in Java 8
      Type Parameters:
      A - input parameter type 1
      B - input parameter type 2
      C - input parameter type 3
      D - input parameter type 4
      E - input parameter type 5
      F - input parameter type 6
      Parameters:
      answer - interface to the answer - a void method
      Returns:
      the answer object to use
      Since:
      2.26.0