return false;
}
private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject,
final Method method, Object[] arguments) throws Throwable {
MockitoInvocationHandler mockHandler = invocationHandler.getHandler();
final FilteredCGLIBProxyRealMethod cglibProxyRealMethod = new FilteredCGLIBProxyRealMethod(new RealMethod() {
private static final long serialVersionUID = 4564320968038564170L;
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.
*/
final Class<?> type = Whitebox.getType(interceptionObject);
final boolean isFinalSystemClass = type.getName().startsWith("java.")
&& Modifier.isFinal(type.getModifiers());
if (!isFinalSystemClass) {
MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true);
}
try {
return method.invoke(target, arguments);
} catch (InvocationTargetException e) {
SafeExceptionRethrower.safeRethrow(e.getCause());
}
return null;
}
});
// }
Invocation invocation = new Invocation(interceptionObject, new DelegatingMethod(method), arguments,
SequenceNumber.next(), cglibProxyRealMethod) {
private static final long serialVersionUID = -3679957412502758558L;
/**
* We need to override this method because normally the String
* "method" is assembled by calling the "qualifiedName" method but
* this is not possible in our case. The reason is that the
* qualifiedName method does
*
* <pre>
* new MockUtil().getMockName(mock)
* </pre>
*
* which later will call the "isMockitoMock" method which will
* return false and an exception will be thrown. The reason why
* "isMockitoMock" returns false is that the mock is not created by
* the Mockito CGLib Enhancer in case of static methods.
*/
@Override
protected String toString(@SuppressWarnings("rawtypes") List<Matcher> matchers, PrintSettings printSettings) {
MatchersPrinter matchersPrinter = new MatchersPrinter();
String method = Whitebox.getType(getMock()).getName() + "." + getMethodName();
String invocation = method + matchersPrinter.getArgumentsLine(matchers, printSettings);
if (printSettings.isMultiline()
|| (!matchers.isEmpty() && invocation.length() > Whitebox.<Integer> getInternalState(
Invocation.class, "MAX_LINE_LENGTH"))) {
return method + matchersPrinter.getArgumentsBlock(matchers, printSettings);
} else {
return invocation;
}
}
};
try {
return mockHandler.handle(invocation);
} catch (NotAMockException e) {
if(invocation.getMock().getClass().getName().startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) {
return invocation.callRealMethod();
} else {
throw e;