org.mockito
public class AdditionalAnswers extends Object
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 from AnswerFunctionalInterfaces
to provide a neater way to write custom answers that either return a value or are void.
See factory methods for more information : returnsFirstArg()
, returnsSecondArg()
,
returnsLastArg()
, returnsArgAt(int)
, answer(org.mockito.stubbing.Answer1
and answerVoid(org.mockito.stubbing.VoidAnswer1)
Constructor and Description |
---|
AdditionalAnswers() |
Modifier and Type | Method and 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(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(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 <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
|
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> |
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 <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> |
returnsElementsOf(Collection<?> elements)
Returns elements of the collection.
|
static <T> Answer<T> |
returnsFirstArg()
Returns the first parameter of an invocation.
|
static <T> Answer<T> |
returnsLastArg()
Returns the last parameter of an invocation.
|
static <T> Answer<T> |
returnsSecondArg()
Returns the second parameter of an invocation.
|
public static <T> Answer<T> returnsFirstArg()
This additional answer could be used at stub time using the
then|do|will
methods. For example :
Answer
given(carKeyFob.authenticate(carKey)).will(returnsFirstArg());
doAnswer(returnsFirstArg()).when(carKeyFob).authenticate(carKey)
T
- Return type of the invocation.public static <T> Answer<T> returnsSecondArg()
This additional answer could be used at stub time using the
then|do|will
methods. For example :
Answer
given(trader.apply(leesFormula, onCreditDefaultSwap)).will(returnsSecondArg());
doAnswer(returnsSecondArg()).when(trader).apply(leesFormula, onCreditDefaultSwap)
T
- Return type of the invocation.public static <T> Answer<T> returnsLastArg()
This additional answer could be used at stub time using the
then|do|will
methods. For example :
Answer
given(person.remember(dream1, dream2, dream3, dream4)).will(returnsLastArg());
doAnswer(returnsLastArg()).when(person).remember(dream1, dream2, dream3, dream4)
T
- Return type of the invocation.public static <T> Answer<T> returnsArgAt(int position)
This additional answer could be used at stub time using the
then|do|will
methods. For example :
Answer
given(person.remember(dream1, dream2, dream3, dream4)).will(returnsArgAt(3));
doAnswer(returnsArgAt(3)).when(person).remember(dream1, dream2, dream3, dream4)
T
- Return type of the invocation.position
- index of the argument from the list of arguments.public static <T> Answer<T> delegatesTo(Object delegate)
Useful for spies or partial mocks of objects that are difficult to mock or spy using the usual spy API. Possible use cases:
The difference with 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.
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 to 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);
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.public static <T> Answer<T> returnsElementsOf(Collection<?> elements)
//this:
when(mock.foo()).thenReturn(1, 2, 3);
//is equivalent to:
when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3)));
elements
- The collection of elements to return.@Incubating public static <T,A> Answer<T> answer(Answer1<T,A> answer)
T
- return typeA
- input parameter type 1answer
- interface to the answer - which is expected to return something@Incubating public static <A> Answer<Void> answerVoid(VoidAnswer1<A> answer)
A
- input parameter type 1answer
- interface to the answer - a void method@Incubating public static <T,A,B> Answer<T> answer(Answer2<T,A,B> answer)
T
- return typeA
- input parameter type 1B
- input parameter type 2answer
- interface to the answer - which is expected to return something@Incubating public static <A,B> Answer<Void> answerVoid(VoidAnswer2<A,B> answer)
A
- input parameter type 1B
- input parameter type 2answer
- interface to the answer - a void method@Incubating public static <T,A,B,C> Answer<T> answer(Answer3<T,A,B,C> answer)
T
- return typeA
- input parameter type 1B
- input parameter type 2C
- input parameter type 3answer
- interface to the answer - which is expected to return something@Incubating public static <A,B,C> Answer<Void> answerVoid(VoidAnswer3<A,B,C> answer)
A
- input parameter type 1B
- input parameter type 2C
- input parameter type 3answer
- interface to the answer - a void method@Incubating public static <T,A,B,C,D> Answer<T> answer(Answer4<T,A,B,C,D> answer)
T
- return typeA
- input parameter type 1B
- input parameter type 2C
- input parameter type 3D
- input parameter type 4answer
- interface to the answer - which is expected to return something@Incubating public static <A,B,C,D> Answer<Void> answerVoid(VoidAnswer4<A,B,C,D> answer)
A
- input parameter type 1B
- input parameter type 2C
- input parameter type 3D
- input parameter type 4answer
- interface to the answer - a void method@Incubating public static <T,A,B,C,D,E> Answer<T> answer(Answer5<T,A,B,C,D,E> answer)
T
- return typeA
- input parameter type 1B
- input parameter type 2C
- input parameter type 3D
- input parameter type 4E
- input parameter type 5answer
- interface to the answer - which is expected to return something@Incubating public static <A,B,C,D,E> Answer<Void> answerVoid(VoidAnswer5<A,B,C,D,E> answer)
A
- input parameter type 1B
- input parameter type 2C
- input parameter type 3D
- input parameter type 4E
- input parameter type 5answer
- interface to the answer - a void method