Search in sources :

Example 1 with CleanTraceRealMethod

use of org.mockito.internal.invocation.realmethod.CleanTraceRealMethod in project mockito by mockito.

the class CleanTraceRealMethodTest method shouldRemoveMockitoInternalsFromStackTraceWhenRealMethodThrows.

@Test
public void shouldRemoveMockitoInternalsFromStackTraceWhenRealMethodThrows() throws Throwable {
    //given
    CleanTraceRealMethod realMethod = new CleanTraceRealMethod(new RealMethod() {

        public Object invoke(Object target, Object[] arguments) throws Throwable {
            return new Foo().throwSomething();
        }
    });
    //when
    try {
        realMethod.invoke(null, null);
        fail();
    //then
    } catch (Exception e) {
        Assertions.assertThat(e).has(methodInStackTraceAt(0, "throwSomething"));
        Assertions.assertThat(e).has(methodInStackTraceAt(1, "invoke"));
        Assertions.assertThat(e).has(methodInStackTraceAt(2, "shouldRemoveMockitoInternalsFromStackTraceWhenRealMethodThrows"));
    }
}
Also used : CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod) RealMethod(org.mockito.internal.invocation.realmethod.RealMethod) Test(org.junit.Test)

Example 2 with CleanTraceRealMethod

use of org.mockito.internal.invocation.realmethod.CleanTraceRealMethod in project powermock by powermock.

the class MethodInterceptorFilter method intercept.

public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    if (isEqualsMethod(method)) {
        return proxy == args[0];
    } else if (isHashCodeMethod(method)) {
        return hashCodeForMock(proxy);
    } else if (acrossJVMSerializationFeature.isWriteReplace(method)) {
        return acrossJVMSerializationFeature.writeReplace(proxy);
    }
    MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy);
    new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
    MockitoMethod mockitoMethod = createMockitoMethod(method);
    CleanTraceRealMethod realMethod = new CleanTraceRealMethod(mockitoMethodProxy);
    Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod, new LocationImpl());
    return handler.handle(invocation);
}
Also used : MockitoMethod(org.mockito.internal.invocation.MockitoMethod) Invocation(org.mockito.invocation.Invocation) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod) LocationImpl(org.mockito.internal.debugging.LocationImpl) MockitoMethodProxy(org.mockito.internal.creation.util.MockitoMethodProxy) InvocationImpl(org.mockito.internal.invocation.InvocationImpl)

Example 3 with CleanTraceRealMethod

use of org.mockito.internal.invocation.realmethod.CleanTraceRealMethod in project powermock by powermock.

the class MockitoMethodInvocationControl method performIntercept.

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 RealMethod() {

        private static final long serialVersionUID = 4564320968038564170L;

        @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.
                     */
            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 InvocationImpl(interceptionObject, new DelegatingMethod(method), arguments, SequenceNumber.next(), cglibProxyRealMethod, new LocationImpl()) {

        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;
    }
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Invocation(org.mockito.invocation.Invocation) NotAMockException(org.mockito.exceptions.misusing.NotAMockException) InvocationImpl(org.mockito.internal.invocation.InvocationImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod) DelegatingMethod(org.mockito.internal.creation.DelegatingMethod) LocationImpl(org.mockito.internal.debugging.LocationImpl) InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) RealMethod(org.mockito.internal.invocation.realmethod.RealMethod) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)

Aggregations

CleanTraceRealMethod (org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)3 LocationImpl (org.mockito.internal.debugging.LocationImpl)2 InvocationImpl (org.mockito.internal.invocation.InvocationImpl)2 RealMethod (org.mockito.internal.invocation.realmethod.RealMethod)2 Invocation (org.mockito.invocation.Invocation)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Test (org.junit.Test)1 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)1 NotAMockException (org.mockito.exceptions.misusing.NotAMockException)1 InternalMockHandler (org.mockito.internal.InternalMockHandler)1 DelegatingMethod (org.mockito.internal.creation.DelegatingMethod)1 MockitoMethodProxy (org.mockito.internal.creation.util.MockitoMethodProxy)1 MockitoMethod (org.mockito.internal.invocation.MockitoMethod)1 MockHandler (org.mockito.invocation.MockHandler)1