public class ReturnsArgumentAt extends java.lang.Object implements Answer<java.lang.Object>, ValidableAnswer, java.io.Serializable
The argumentIndex
represents the index in the argument array of the invocation.
If this number equals -1 then the last argument is returned.
AdditionalAnswers
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
LAST_ARGUMENT |
private static long |
serialVersionUID |
private int |
wantedArgumentPosition |
Constructor and Description |
---|
ReturnsArgumentAt(int wantedArgumentPosition)
Build the identity answer to return the argument at the given position in the argument array.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
answer(InvocationOnMock invocation) |
private java.lang.Class<?> |
inferArgumentType(Invocation invocation,
int argumentIndex) |
private int |
inferWantedArgumentPosition(InvocationOnMock invocation) |
private void |
validateArgumentTypeCompatibility(Invocation invocation,
int argumentPosition) |
void |
validateFor(InvocationOnMock invocation)
Validation of the answer at stub time for the given invocation.
|
private void |
validateIndexWithinInvocationRange(InvocationOnMock invocation,
int argumentPosition) |
private boolean |
wantedArgIndexIsVarargAndSameTypeAsReturnType(java.lang.reflect.Method method,
int argumentPosition) |
private boolean |
wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation,
int argumentPosition) |
private static final long serialVersionUID
public static final int LAST_ARGUMENT
private final int wantedArgumentPosition
public ReturnsArgumentAt(int wantedArgumentPosition)
wantedArgumentPosition
- The position of the argument identity to return in the invocation. Using -1
indicates the last argument (LAST_ARGUMENT
).public java.lang.Object answer(InvocationOnMock invocation) throws java.lang.Throwable
public void validateFor(InvocationOnMock invocation)
ValidableAnswer
This method will be called by Mockito.
The implementation must throw an MockitoException to indicate that this answer is not valid for the given invocation. If the validation succeed the implementation must simply return without throwing.
validateFor
in interface ValidableAnswer
invocation
- The stubbed invocationprivate int inferWantedArgumentPosition(InvocationOnMock invocation)
private void validateIndexWithinInvocationRange(InvocationOnMock invocation, int argumentPosition)
private void validateArgumentTypeCompatibility(Invocation invocation, int argumentPosition)
private boolean wantedArgIndexIsVarargAndSameTypeAsReturnType(java.lang.reflect.Method method, int argumentPosition)
private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition)
private java.lang.Class<?> inferArgumentType(Invocation invocation, int argumentIndex)