@Override public Object invoke(Object target, Object[] arguments) throws Throwable { /* * Instruct the MockGateway to don't intercept the next call. * The reason is that when Mockito is spying on objects it * should call the "real method" (which is proxied by Mockito * anyways) so that we don't end up in here one more time which * causes infinite recursion. This should not be done if the * interceptionObject is a final system class because these are * never caught by the Mockito proxy. */ notMockNextCallIfRequired(); try { return method.invoke(target, arguments); } catch (InvocationTargetException e) { SafeExceptionRethrower.safeRethrow(e.getCause()); } return null; }
private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject, final Method method, Object[] arguments) throws Throwable { MockHandler mockHandler = invocationHandler.getHandler(); final CleanTraceRealMethod cglibProxyRealMethod = new CleanTraceRealMethod(new MockedRealMethod(method, interceptionObject)); Invocation invocation = new InvocationImpl( interceptionObject, new DelegatingMethod(method), arguments, SequenceNumber.next(), cglibProxyRealMethod) { private static final long serialVersionUID = -3679957412502758558L; @Override public String toString() { return new ToStringGenerator().generate(getMock(), getMethod(), getArguments()); } }; try { return replaceMatchersBinderIfNeeded(mockHandler).handle(invocation); } catch (NotAMockException e) { if(invocation.getMock().getClass().getName().startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) { return invocation.callRealMethod(); } else { throw e; } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e); throw e; } }
private boolean isNextCallShouldNotBeMocked(Class<?> type) { return !isFinalSystemJavaClass(type); }
private void notMockNextCallIfRequired() { final Class<?> type = Whitebox.getType(interceptionObject); if (isNextCallShouldNotBeMocked(type)) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } }