Search in sources :

Example 1 with MockitoMethodInvocationControl

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

the class LocationFromStackTraceTest method should_filter_extra_elements_in_stack_when_mocking_static_method.

@Test
public void should_filter_extra_elements_in_stack_when_mocking_static_method() throws Exception {
    mockStatic(SomethingWithStaticMethod.class);
    when(SomethingWithStaticMethod.doStaticOne()).thenReturn("Something else 1");
    when(SomethingWithStaticMethod.doStaticTwo()).thenReturn("Something else 2");
    doNothing().when(SomethingWithStaticMethod.class, "doStaticVoid");
    MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(SomethingWithStaticMethod.class);
    MockHandlerAdaptor mockHandlerAdaptor = invocationControl.getMockHandlerAdaptor();
    Object mock = mockHandlerAdaptor.getMock();
    MockHandler<?> mockHandler = MockUtil.getMockHandler(mock);
    InvocationContainerImpl invocationContainer = (InvocationContainerImpl) mockHandler.getInvocationContainer();
    List<Stubbing> stubbings = new ArrayList<Stubbing>(invocationContainer.getStubbingsAscending());
    assertThat(stubbings.size(), is(3));
    Location static1Location = stubbings.get(0).getInvocation().getLocation();
    assertThat(static1Location.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:37)"));
    Location static2Location = stubbings.get(1).getInvocation().getLocation();
    assertThat(static2Location.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:38)"));
    Location staticVoidLocation = stubbings.get(2).getInvocation().getLocation();
    assertThat(staticVoidLocation.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:39)"));
    // Removing these calls and the three Location assertions above will cause the test to fail due to the
    // strict stubs.  Without the alterations to StackTraceCleanerProvider, the failure messages will contain
    // an item in the stack trace inside the powermock libraries.
    assertThat(SomethingWithStaticMethod.doStaticOne(), is("Something else 1"));
    assertThat(SomethingWithStaticMethod.doStaticTwo(), is("Something else 2"));
    SomethingWithStaticMethod.doStaticVoid();
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) ArrayList(java.util.ArrayList) MockitoMethodInvocationControl(org.powermock.api.mockito.invocation.MockitoMethodInvocationControl) MockHandlerAdaptor(org.powermock.api.mockito.invocation.MockHandlerAdaptor) Location(org.mockito.invocation.Location) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with MockitoMethodInvocationControl

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

the class DefaultMockCreator method createMethodInvocationControl.

@SuppressWarnings("unchecked")
private <T> MockData<T> createMethodInvocationControl(Class<T> type, Method[] methods, Object delegator, MockSettings mockSettings) {
    final T mock = Mockito.mock(type, mockSettings != null ? mockSettings : Mockito.withSettings());
    cacheMockClass(mock.getClass());
    return new MockData<T>(new MockitoMethodInvocationControl(delegator, mock, methods), mock);
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)

Example 3 with MockitoMethodInvocationControl

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

Example 4 with MockitoMethodInvocationControl

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

the class PowerMockMaker method getRealMock.

private Object getRealMock(final Object mock) {
    final MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(mock);
    final Object realMock;
    if (invocationControl == null) {
        realMock = mock;
    } else {
        realMock = invocationControl.getMockHandlerAdaptor().getMock();
    }
    return realMock;
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)

Example 5 with MockitoMethodInvocationControl

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

the class PowerMockitoStubberImpl method when.

@Override
public void when(Class<?> classMock) {
    MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(classMock);
    final Object mock = invocationControl.getMockHandlerAdaptor().getMock();
    stubber.when(mock);
}
Also used : MockitoMethodInvocationControl(org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)

Aggregations

MockitoMethodInvocationControl (org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)6 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)1 Location (org.mockito.invocation.Location)1 Stubbing (org.mockito.stubbing.Stubbing)1 MockitoNewInvocationControl (org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl)1 MockHandlerAdaptor (org.powermock.api.mockito.invocation.MockHandlerAdaptor)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1