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();
}
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);
}
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();
}
}
}
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;
}
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);
}
Aggregations