private static boolean isEqualsMethod(MockInvocation mockInvocation) { return "equals".equals(mockInvocation.getMethod().getName()); }
MockInvocation(Object object, String methodName, Class<?>... sig) { this.object = object; this.methodName = methodName; this.sig = sig; init(); }
/** * {@link #noMockito} is wrapped into it's own static class to make sure it is initialized not earlier than * {@link #calledFromMockito()} is called for the first time. */ private static final class NoMockito { static final boolean noMockito = Package.getPackage("org.mockito") == null; private NoMockito() {} } public static final Object PROCEED = new Object();
private static Object doMethodCall(Object object, String methodName, Object[] args, Class<?>[] sig, String returnTypeAsString) throws Throwable { if (!shouldMockMethod(methodName, sig)) { return PROCEED; } MockInvocation mockInvocation = new MockInvocation(object, methodName, sig); MethodInvocationControl methodInvocationControl = mockInvocation.getMethodInvocationControl(); Object returnValue = null; // The following describes the equals non-static method. if (isEqualsMethod(mockInvocation) && !isStaticMethod(mockInvocation)) { returnValue = tryHandleEqualsMethod(mockInvocation); } if (returnValue != null) { return returnValue; } return doMethodCall(object, args, returnTypeAsString, mockInvocation, methodInvocationControl); }
private void init() { if (object instanceof Class<?>) { objectType = (Class<?>) object; methodInvocationControl = MockRepository.getStaticMethodInvocationControl(objectType); } else { final Class<?> type = object.getClass(); UnproxiedType unproxiedType = WhiteboxImpl.getUnproxiedType(type); objectType = unproxiedType.getOriginalType(); methodInvocationControl = MockRepository.getInstanceMethodInvocationControl(object); } method = findMethodToInvoke(methodName, sig, objectType); }
private static boolean isStaticMethod(MockInvocation mockInvocation) { return Modifier.isStatic(mockInvocation.getMethod().getModifiers()); }
private static Object tryHandleEqualsMethod(MockInvocation mockInvocation) { // Fix for Issue http://code.google.com/p/powermock/issues/detail?id=88 // For some reason the method call to equals() on final methods is // intercepted and during the further processing in Mockito the same // equals() method is called on the same instance. A StackOverflowError // is the result. The following fix changes this by checking if the // method to be called is a final equals() method. In that case the // original method is called by returning PROCEED. if (mockInvocation.getMethod().getParameterTypes().length == 1 && mockInvocation.getMethod().getParameterTypes()[0] == Object.class && Modifier.isFinal(mockInvocation.getMethod().getModifiers())) { return PROCEED; } if (calledFromMockito()){ return PROCEED; } return null; }