public static void validateReturnValueFor(InvocationOnMock invocation, Object returnedValue) throws Throwable { InvocationInfo invocationInfo = new InvocationInfo(invocation); if (returnedValue != null && !invocationInfo.isValidReturnType(returnedValue.getClass())) { throw wrongTypeReturnedByDefaultAnswer( invocation.getMock(), invocationInfo.printMethodReturnType(), returnedValue.getClass().getSimpleName(), invocationInfo.getMethodName()); } } }
@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(); } } }
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) { 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()); } }
@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); } } }