-
Notifications
You must be signed in to change notification settings - Fork 586
Mockito Usage Legacy
Note: PowerMock 1.2.5 only works with Mockito 1.7. [PowerMock 1.3+ works with 1.8+](MockitoUsage)**
Basically, PowerMock provides a class called "PowerMockito" for creating mock object/class and initiating verification, everything else you can still use Mockito to setup and verify expectation (e.g. when(), times(), anyInt()).
All usages require @RunWith(PowerMockRunner.class)
and @PrepareForTest
annotated at class level.
How to mock and stub:
- Add
@PrepareForTest
at class level.
@PrepareForTest(Static.class); // Static.class contains static methods
- Call
PowerMockito.mockStatic()
to mock a static class (usePoweMockito.mockStaticPartial(class, method)
to mock a specific method):
PowerMockito.mockStatic(Static.class);
- Just use Mockito.when() to setup your expectation:
Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);
- Call
PowerMockito.verifyStatic()
to start verifying behavior (Important: You need to callverifyStatic()
per method verification):
PowerMockito.verifyStatic(Static.class);
- Use EasyMock-like semantic to verify behavior:
Static.firstStaticMethod(param);
Mockito matchers are may still applied to a PowerMock mock. For example, using custom argument matchers per mocked static method:
PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());
You can still use Mockito.VerificationMode (e.g Mockito.times(x)) with PowerMockito.verifyStatic()
:
PowerMockito.verifyStatic(Static.class, Mockito.times(1));
This is not yet supported in current release 1.2.5
@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class)
public class YourTestCase {
@Test
public void testMethodThatCallsStaticMethod() {
// mock all the static methods in a class called "Static"
PowerMockito.mockStatic(Static.class);
// use Mockito to set up your expectation
Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);
Mockito.when(Static.secondStaticMethod()).thenReturn(123);
// execute your test
classCallStaticMethodObj.execute();
// Different from Mockito, always use PowerMockito.verifyStatic() first
PowerMockito.verifyStatic(Static.class, Mockito.times(2));
// Use EasyMock-like verification semantic per static method invocation
Static.firstStaticMethod(param);
// Remember to call verifyStatic() again
PowerMockito.verifyStatic(Static.class); // default is once
Static.secondStaticMethod();
// Again, remember to call verifyStatic()
PowerMockito.verifyStatic(Static.class, Mockito.never());
Static.thirdStaticMethod();
}
}
You can use PowerMock to partially mock a method which must be non-final.
- Use PowerMockito.mockPartial(): (You can also use Annotation
@Mock("methodToMock")
orPowerMockito.mock()
for a method with specific parameters)
PartialMockClass mockObj = PowerMockito.mockPartial(PartialMockClass.class, "methodToMock");
- Just use
Mockito.when()
to setup expectation:
Mockito.when(mockObj.methodToMock()).thenReturn(123);
Just use Mockito.vertify() for verification:
Mockito.verify(mockObj, times(2)).methodToMock();
Mockito matchers are may still applied to a PowerMock mock:
Mockito.verify(mockObj).methodToMockToo(Mockito.anyInt());
Just use Mockito semantic of setting up void method stub:
Mockito.doThrow(new RuntimeException("TEST")).when(mockObj).methodToMock();
@RunWith(PowerMockRunner.class)
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void testPartialMock() {
// create a partially mocked object for method "methodToMock"
PartialMockClass mockObj = PowerMockito.mockPartial(PartialMockClass.class, "methodToMock");
// use Mockito to set up your expectation
Mockito.when(mockObj.methodToMock()).thenReturn(value);
// execute your test
classCallPartialMockObj.execute();
// Use Mockito.verify() to verify result
Mockito.verify(mockObj, times(2)).methodToMock();
}
}