Search in sources :

Example 6 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class AbstractByteBuddyMockMakerTest method should_allow_serialization.

@Test
public void should_allow_serialization() throws Exception {
    SerializableClass proxy = mockMaker.createMock(serializableSettingsFor(SerializableClass.class, SerializableMode.BASIC), dummyHandler());
    SerializableClass serialized = SimpleSerializationUtil.serializeAndBack(proxy);
    assertThat(serialized).isNotNull();
    MockHandler handlerOne = mockMaker.getHandler(proxy);
    MockHandler handlerTwo = mockMaker.getHandler(serialized);
    assertThat(handlerOne).isNotSameAs(handlerTwo);
}
Also used : MockHandler(org.mockito.invocation.MockHandler) Test(org.junit.Test) AbstractMockMakerTest(org.mockito.internal.creation.AbstractMockMakerTest)

Example 7 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class MockUtil method resetMock.

public static void resetMock(Object mock) {
    MockHandler oldHandler = getMockHandler(mock);
    MockCreationSettings settings = oldHandler.getMockSettings();
    MockHandler newHandler = createMockHandler(settings);
    mock = resolve(mock);
    mockMaker.resetMock(mock, newHandler, settings);
}
Also used : MockCreationSettings(org.mockito.mock.MockCreationSettings) MockHandlerFactory.createMockHandler(org.mockito.internal.handler.MockHandlerFactory.createMockHandler) MockHandler(org.mockito.invocation.MockHandler)

Example 8 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class MockUtil method createMock.

public static <T> T createMock(MockCreationSettings<T> settings) {
    MockHandler mockHandler = createMockHandler(settings);
    Object spiedInstance = settings.getSpiedInstance();
    T mock;
    if (spiedInstance != null) {
        mock = mockMaker.createSpy(settings, mockHandler, (T) spiedInstance).orElseGet(() -> {
            T instance = mockMaker.createMock(settings, mockHandler);
            new LenientCopyTool().copyToMock(spiedInstance, instance);
            return instance;
        });
    } else {
        mock = mockMaker.createMock(settings, mockHandler);
    }
    return mock;
}
Also used : MockHandlerFactory.createMockHandler(org.mockito.internal.handler.MockHandlerFactory.createMockHandler) MockHandler(org.mockito.invocation.MockHandler) LenientCopyTool(org.mockito.internal.util.reflection.LenientCopyTool)

Example 9 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class AbstractByteBuddyMockMakerTest method should_mocks_have_different_interceptors.

@Test
public void should_mocks_have_different_interceptors() throws Exception {
    SomeClass mockOne = mockMaker.createMock(settingsFor(SomeClass.class), dummyH());
    SomeClass mockTwo = mockMaker.createMock(settingsFor(SomeClass.class), dummyH());
    MockHandler handlerOne = mockMaker.getHandler(mockOne);
    MockHandler handlerTwo = mockMaker.getHandler(mockTwo);
    assertThat(handlerOne).isNotSameAs(handlerTwo);
}
Also used : InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) Test(org.junit.Test)

Example 10 with MockHandler

use of org.mockito.invocation.MockHandler 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

MockHandler (org.mockito.invocation.MockHandler)14 Test (org.junit.Test)5 InternalMockHandler (org.mockito.internal.InternalMockHandler)5 MockHandlerFactory.createMockHandler (org.mockito.internal.handler.MockHandlerFactory.createMockHandler)4 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)3 NotAMockException (org.mockito.exceptions.misusing.NotAMockException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MockingDetails (org.mockito.MockingDetails)2 MockHandlerImpl (org.mockito.internal.handler.MockHandlerImpl)2 MockingProgress (org.mockito.internal.progress.MockingProgress)2 MockAwareVerificationMode (org.mockito.internal.verification.MockAwareVerificationMode)2 MockCreationSettings (org.mockito.mock.MockCreationSettings)2 VerificationMode (org.mockito.verification.VerificationMode)2 List (java.util.List)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 AbstractMockMakerTest (org.mockito.internal.creation.AbstractMockMakerTest)1 DelegatingMethod (org.mockito.internal.creation.DelegatingMethod)1 LocationImpl (org.mockito.internal.debugging.LocationImpl)1 InvocationImpl (org.mockito.internal.invocation.InvocationImpl)1 CleanTraceRealMethod (org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)1