Search in sources :

Example 1 with InternalMockHandler

use of org.mockito.internal.InternalMockHandler in project mockito by mockito.

the class MockUtil method resetMock.

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

Example 2 with InternalMockHandler

use of org.mockito.internal.InternalMockHandler in project powermock by powermock.

the class CglibMockMaker method createMock.

public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    InternalMockHandler mockitoHandler = cast(handler);
    new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
    return new ClassImposterizer(new DefaultInstantiatorProvider().getInstantiator(settings)).imposterise(new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
Also used : DefaultInstantiatorProvider(org.mockito.internal.creation.instance.DefaultInstantiatorProvider) InternalMockHandler(org.mockito.internal.InternalMockHandler)

Example 3 with InternalMockHandler

use of org.mockito.internal.InternalMockHandler in project powermock by powermock.

the class DefaultMockCreator method createMethodInvocationControl.

@SuppressWarnings("unchecked")
private static <T> MockData<T> createMethodInvocationControl(final String mockName, Class<T> type, Method[] methods, boolean isSpy, Object delegator, MockSettings mockSettings) {
    final MockSettingsImpl settings;
    if (mockSettings == null) {
        // We change the context classloader to the current CL in order for the Mockito
        // framework to load it's plugins (such as MockMaker) correctly.
        final ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DefaultMockCreator.class.getClassLoader());
        try {
            settings = (MockSettingsImpl) Mockito.withSettings();
            Plugins.getMockMaker();
        } finally {
            Thread.currentThread().setContextClassLoader(originalCL);
        }
    } else {
        settings = (MockSettingsImpl) mockSettings;
    }
    if (isSpy) {
        settings.defaultAnswer(Mockito.CALLS_REAL_METHODS);
    }
    settings.setMockName(new MockNameImpl(mockName));
    settings.setTypeToMock(type);
    InternalMockHandler mockHandler = MockHandlerFactory.createMockHandler(settings);
    MethodInterceptorFilter filter = new PowerMockMethodInterceptorFilter(mockHandler, settings);
    final T mock = new ClassImposterizer(new DefaultInstantiatorProvider().getInstantiator(settings)).imposterise(filter, type);
    ClassLoader classLoader = mock.getClass().getClassLoader();
    if (classLoader instanceof MockClassLoader) {
        MockClassLoader mcl = (MockClassLoader) classLoader;
        mcl.cache(mock.getClass());
    }
    final MockitoMethodInvocationControl invocationControl = new MockitoMethodInvocationControl(filter, isSpy && delegator == null ? new Object() : delegator, mock, methods);
    return new MockData<T>(invocationControl, mock);
}
Also used : MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) ClassImposterizer(org.powermock.api.mockito.repackaged.ClassImposterizer) MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl) MockNameImpl(org.mockito.internal.util.MockNameImpl) DefaultInstantiatorProvider(org.mockito.internal.creation.instance.DefaultInstantiatorProvider) MockClassLoader(org.powermock.core.classloader.MockClassLoader) InternalMockHandler(org.mockito.internal.InternalMockHandler) MethodInterceptorFilter(org.powermock.api.mockito.repackaged.MethodInterceptorFilter)

Aggregations

InternalMockHandler (org.mockito.internal.InternalMockHandler)3 DefaultInstantiatorProvider (org.mockito.internal.creation.instance.DefaultInstantiatorProvider)2 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)1 MockHandlerFactory.createMockHandler (org.mockito.internal.handler.MockHandlerFactory.createMockHandler)1 MockNameImpl (org.mockito.internal.util.MockNameImpl)1 MockHandler (org.mockito.invocation.MockHandler)1 MockCreationSettings (org.mockito.mock.MockCreationSettings)1 MockitoMethodInvocationControl (org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)1 ClassImposterizer (org.powermock.api.mockito.repackaged.ClassImposterizer)1 MethodInterceptorFilter (org.powermock.api.mockito.repackaged.MethodInterceptorFilter)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1