public InvocationInfo(InvocationOnMock theInvocation) { this.method = theInvocation.getMethod(); }
private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition) { if (argumentPosition < 0) { return false; } if (!invocation.getMethod().isVarArgs()) { return invocation.getArguments().length > argumentPosition; } // for all varargs accepts positive ranges return true; }
public Object answer(InvocationOnMock invocation) throws Throwable { Method method = invocation.getMethod() ; return method.invoke(delegatedObject, invocation.getArguments()); } }
private static StringBuilder possibleArgumentTypesOf(InvocationOnMock invocation) { Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes(); if (parameterTypes.length == 0) { return new StringBuilder("the method has no arguments.\n"); } StringBuilder stringBuilder = new StringBuilder("the possible argument indexes for this method are :\n"); for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) { stringBuilder.append(" [").append(i); if (invocation.getMethod().isVarArgs() && i == parameterTypesLength - 1) { stringBuilder.append("+] ").append(parameterTypes[i].getComponentType().getSimpleName()).append(" <- Vararg").append("\n"); } else { stringBuilder.append("] ").append(parameterTypes[i].getSimpleName()).append("\n"); } } return stringBuilder; }
private boolean argumentPositionInRange(InvocationOnMock invocation) { int actualArgumentPosition = actualArgumentPosition(invocation); if (actualArgumentPosition < 0) { return false; } if (!invocation.getMethod().isVarArgs()) { return invocation.getArguments().length > actualArgumentPosition; } // for all varargs accepts positive ranges return true; }
public Object answer(InvocationOnMock currentInvocation) throws Throwable { if (isToStringMethod(currentInvocation.getMethod())) { return "SmartNull returned by this unstubbed method call on a mock:\n" + unstubbedInvocation.toString(); } throw smartNullPointerException(unstubbedInvocation.toString(), location); } }
public Object answer(InvocationOnMock invocation) throws Throwable { if (Modifier.isAbstract(invocation.getMethod().getModifiers())) { return RETURNS_DEFAULTS.answer(invocation); } return invocation.callRealMethod(); }
public Object answer(InvocationOnMock invocation) throws Throwable { Object ret = delegate.answer(invocation); if (ret != null) { return ret; } return returnValueFor(invocation.getMethod().getReturnType()); }
public Object answer(InvocationOnMock invocation) throws Throwable { Object ret = delegate.answer(invocation); if (ret != null) { return ret; } Class<?> returnType = invocation.getMethod().getReturnType(); return returnValueFor(returnType); }
public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); if (defaultReturnValue != null) { return defaultReturnValue; } Class<?> type = invocation.getMethod().getReturnType(); if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { final Location location = new LocationImpl(); return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); } return null; }
public Object answer(InvocationOnMock invocation) throws Throwable { Object ret = delegate.answer(invocation); if (ret != null) { return ret; } Class<?> returnType = invocation.getMethod().getReturnType(); return returnValueFor(returnType); }
public Object answer(InvocationOnMock invocation) throws Throwable { Object ret = delegate.answer(invocation); if (ret != null) { return ret; } return returnValueFor(invocation.getMethod().getReturnType()); }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { int argumentPosition = inferWantedArgumentPosition(invocation); validateIndexWithinInvocationRange(invocation, argumentPosition); if (wantedArgIndexIsVarargAndSameTypeAsReturnType(invocation.getMethod(), argumentPosition)) { // answer raw vararg array argument return ((Invocation) invocation).getRawArguments()[argumentPosition]; } // answer expanded argument at wanted position return invocation.getArgument(argumentPosition); }
public Object answer(InvocationOnMock currentInvocation) throws Throwable { if (new ObjectMethodsGuru().isToString(currentInvocation.getMethod())) { return "SmartNull returned by this unstubbed method call on a mock:\n" + unstubbedInvocation.toString(); } new Reporter().smartNullPointerException(unstubbedInvocation.toString(), location); return null; } }
public Object answer(InvocationOnMock invocation) throws Throwable { Class<?> methodReturnType = invocation.getMethod().getReturnType(); Object mock = invocation.getMock(); Class<?> mockType = MockUtil.getMockHandler(mock).getMockSettings().getTypeToMock(); if (methodReturnType.isAssignableFrom(mockType)) { return invocation.getMock(); } return defaultReturn.returnValueFor(methodReturnType); }
public Object answer(InvocationOnMock invocation) throws Throwable { Class<?> clz = invocation.getMethod().getReturnType(); if (!new MockCreationValidator().isTypeMockable(clz)) { return delegate.answer(invocation); } return getMock(invocation); }
public static MockitoException invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) { return new MockitoException(join( "Invalid argument index for the current invocation of method : ", " -> " + MockUtil.getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", (willReturnLastParameter ? "Last parameter wanted" : "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation), "The index need to be a positive number that indicates a valid position of the argument in the invocation.", "However it is possible to use the -1 value to indicates that the last argument should be returned.", "" )); }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { Object mock = invocation.getMock(); if (invocation.getMethod().getReturnType().isInstance(mock)) { return mock; } else { return RETURNS_DEFAULTS.answer(invocation); } } }
public int invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) { throw new MockitoException( join("Invalid argument index for the current invocation of method : ", " -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", (willReturnLastParameter ? "Last parameter wanted" : "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation), "The index need to be a positive number that indicates a valid position of the argument in the invocation.", "However it is possible to use the -1 value to indicates that the last argument should be returned.", "")); }
public Object answer(InvocationOnMock invocation) throws Throwable { GenericMetadataSupport returnTypeGenericMetadata = actualParameterizedType(invocation.getMock()).resolveGenericReturnType(invocation.getMethod()); Class<?> rawType = returnTypeGenericMetadata.rawType(); if (!mockitoCore().isTypeMockable(rawType)) { return delegate().returnValueFor(rawType); } return deepStub(invocation, returnTypeGenericMetadata); }