@Override public void validateFor(InvocationOnMock invocation) { int argumentPosition = inferWantedArgumentPosition(invocation); validateIndexWithinInvocationRange(invocation, argumentPosition); validateArgumentTypeCompatibility((Invocation) invocation, argumentPosition); }
/** * Build the identity answer to return the argument at the given position in the argument array. * * @param wantedArgumentPosition The position of the argument identity to return in the invocation. * Using <code>-1</code> indicates the last argument. */ public ReturnsArgumentAt(int wantedArgumentPosition) { this.wantedArgumentPosition = checkWithinAllowedRange(wantedArgumentPosition); }
private int actualArgumentPosition(InvocationOnMock invocation) { return returningLastArg() ? lastArgumentIndexOf(invocation) : argumentIndexOf(invocation); }
@Override public Stubber doThrow(Throwable... toBeThrown) { if (toBeThrown == null) { answers.add(new ThrowsException(null)); return this; } for (Throwable throwable : toBeThrown) { answers.add(new ThrowsException(throwable)); } return this; }
@Override public void validateFor(InvocationOnMock invocation) { InvocationInfo invocationInfo = new InvocationInfo(invocation); if (invocationInfo.isVoid()) { throw cannotStubVoidMethodWithAReturnValue(invocationInfo.getMethodName()); } if (returnsNull() && invocationInfo.returnsPrimitive()) { throw wrongTypeOfReturnValue(invocationInfo.printMethodReturnType(), "null", invocationInfo.getMethodName()); } if (!returnsNull() && !invocationInfo.isValidReturnType(returnType())) { throw wrongTypeOfReturnValue(invocationInfo.printMethodReturnType(), printReturnType(), invocationInfo.getMethodName()); } }
private void validateReturnValue(Returns answer, MethodInfo methodInfo) { if (methodInfo.isVoid()) { reporter.cannotStubVoidMethodWithAReturnValue(methodInfo.getMethodName()); } if (answer.returnsNull() && methodInfo.returnsPrimitive()) { reporter.wrongTypeOfReturnValue(methodInfo.printMethodReturnType(), "null", methodInfo.getMethodName()); } if (!answer.returnsNull() && !methodInfo.isValidReturnType(answer.getReturnType())) { reporter.wrongTypeOfReturnValue(methodInfo.printMethodReturnType(), answer.printReturnType(), methodInfo.getMethodName()); } }
private void validateReturnArgIdentity(ReturnsArgumentAt returnsArgumentAt, Invocation invocation) { returnsArgumentAt.validateIndexWithinInvocationRange(invocation); MethodInfo methodInfo = new MethodInfo(invocation); if (!methodInfo.isValidReturnType(returnsArgumentAt.returnedTypeOnSignature(invocation))) { new Reporter().wrongTypeOfArgumentToReturn(invocation, methodInfo.printMethodReturnType(), returnsArgumentAt.returnedTypeOnSignature(invocation), returnsArgumentAt.wantedArgumentPosition()); } }
@Override public Stubber doCallRealMethod() { answers.add(new CallsRealMethods()); return this; } }
/** * 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. * <p> * <pre class="code"><code class="java"> * //this: * when(mock.foo()).thenReturn(1, 2, 3); * * //is equivalent to: * when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3))); * </code></pre> * * @param elements The collection of elements to return. * @return the answer * * @since 1.9.5 */ public static <T> Answer<T> returnsElementsOf(Collection<?> elements) { return (Answer<T>) new ReturnsElementsOf(elements); }
private void validateArgumentTypeCompatibility(Invocation invocation, int argumentPosition) { InvocationInfo invocationInfo = new InvocationInfo(invocation); Class<?> inferredArgumentType = inferArgumentType(invocation, argumentPosition); if (!invocationInfo.isValidReturnType(inferredArgumentType)){ throw wrongTypeOfArgumentToReturn(invocation, invocationInfo.printMethodReturnType(), inferredArgumentType, wantedArgumentPosition); } }
@Override public void validateFor(InvocationOnMock invocation) { if (new InvocationInfo(invocation).isAbstract()) { throw cannotCallAbstractRealMethod(); } } }
@Override public void validateFor(InvocationOnMock invocation) { if (!new InvocationInfo(invocation).isVoid()) { throw onlyVoidMethodsCanBeSetToDoNothing(); } } }
@Override public void validateFor(InvocationOnMock invocation) { if (throwable == null) { throw cannotStubWithNullThrowable(); } if (throwable instanceof RuntimeException || throwable instanceof Error) { return; } if (!new InvocationInfo(invocation).isValidException(throwable)) { throw checkedExceptionInvalid(throwable); } } }
/** * Creates an answer from a functional interface - allows for a strongly typed answer to be created * ideally in Java 8 * @param answer interface to the answer - a void method * @param <A> input parameter type 1 * @return the answer object to use * @since 2.1.0 */ @Incubating public static <A> Answer<Void> answerVoid(VoidAnswer1<A> answer) { return toAnswer(answer); }
@Override public Stubber doNothing() { answers.add(doesNothing()); return this; }
private void validateIndexWithinInvocationRange(InvocationOnMock invocation, int argumentPosition) { if (!wantedArgumentPositionIsValidForInvocation(invocation, argumentPosition)) { throw invalidArgumentPositionRangeAtInvocationTime(invocation, wantedArgumentPosition == LAST_ARGUMENT, wantedArgumentPosition); } }
@Override public OngoingStubbing<T> thenReturn(T value) { return thenAnswer(new Returns(value)); }
/** * Creates an answer from a functional interface - allows for a strongly typed answer to be created * ideally in Java 8 * @param answer interface to the answer - a void method * @param <A> input parameter type 1 * @param <B> input parameter type 2 * @return the answer object to use * @since 2.1.0 */ @Incubating public static <A, B> Answer<Void> answerVoid(VoidAnswer2<A, B> answer) { return toAnswer(answer); }
/** * Creates an answer from a functional interface - allows for a strongly typed answer to be created * ideally in Java 8 * @param answer interface to the answer - which is expected to return something * @param <T> return type * @param <A> input parameter type 1 * @return the answer object to use * @since 2.1.0 */ @Incubating public static <T, A> Answer<T> answer(Answer1<T, A> answer) { return toAnswer(answer); }