Search in sources :

Example 1 with MockHandlerAdaptor

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

Aggregations

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 MockHandlerAdaptor (org.powermock.api.mockito.invocation.MockHandlerAdaptor)1 MockitoMethodInvocationControl (org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1