Search in sources :

Example 1 with MockitoMethodInvocationControl

use of org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl in project powermock by powermock.

the class VerifyNoMoreInteractions method verifyNoMoreInteractions.

private static void verifyNoMoreInteractions(Class<?>... types) {
    for (Class<?> type : types) {
        final MockitoMethodInvocationControl invocationHandler = (MockitoMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(type);
        if (invocationHandler != null) {
            invocationHandler.verifyNoMoreInteractions();
        }
        MockitoNewInvocationControl<?> newInvocationControl = (MockitoNewInvocationControl<?>) MockRepository.getNewInstanceControl(type);
        if (newInvocationControl != null) {
            newInvocationControl.verifyNoMoreInteractions();
        }
    }
}
Also used : MockitoNewInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl) MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)

Example 2 with MockitoMethodInvocationControl

use of org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl in project powermock by powermock.

the class PowerMockitoStubberImpl method prepareForStubbing.

private <T> void prepareForStubbing(T mock) {
    MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(mock);
    addAnswersForStubbing(invocationControl);
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)

Example 3 with MockitoMethodInvocationControl

use of org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl 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)

Example 4 with MockitoMethodInvocationControl

use of org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl in project powermock by powermock.

the class PowerMockitoStubberImpl method when.

/**
     * Supports PowerMockito mocks. If {@code mock} is not a PowerMockito
     * mock it will delegate to Mockito.
     *
     * @see Stubber#when(Object)
     */
@Override
public <T> T when(T instanceMock) {
    MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(instanceMock);
    final T returnValue;
    if (invocationControl == null) {
        returnValue = super.when(instanceMock);
    } else {
        addAnswersForStubbing(invocationControl);
        returnValue = instanceMock;
    }
    return returnValue;
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)

Example 5 with MockitoMethodInvocationControl

use of org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl in project powermock by powermock.

the class PowerMockitoStubberImpl method when.

@Override
public void when(Class<?> classMock) {
    MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(classMock);
    addAnswersForStubbing(invocationControl);
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)

Aggregations

MockitoMethodInvocationControl (org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl)5 InternalMockHandler (org.mockito.internal.InternalMockHandler)1 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)1 DefaultInstantiatorProvider (org.mockito.internal.creation.instance.DefaultInstantiatorProvider)1 MockNameImpl (org.mockito.internal.util.MockNameImpl)1 MockitoNewInvocationControl (org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl)1 ClassImposterizer (org.powermock.api.mockito.repackaged.ClassImposterizer)1 MethodInterceptorFilter (org.powermock.api.mockito.repackaged.MethodInterceptorFilter)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1