|
MockitoUsage
Using PowerMock with MockitoNote: PowerMock 1.2.5 only works with Mockito 1.7. PowerMock 1.3 works with 1.8 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. Mocking Static MethodHow to mock and stub:
@PrepareForTest(Static.class); // Static.class contains static methods PowerMockito.mockStatic(Static.class); Mockito.when(Static.firstStaticMethod(param)).thenReturn(value); How to verify behavior
PowerMockito.verifyStatic(Static.class); Static.firstStaticMethod(param); How to use argument matchersMockito 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()); How to verify exact number of callsYou can still use Mockito.VerificationMode (e.g Mockito.times(x)) with PowerMockito.verifyStatic(): PowerMockito.verifyStatic(Static.class, Mockito.times(1)); How to stub void static method to throw exceptionThis is not yet supported in current release 1.2.5 A full example for mocking, stubbing & verifying static method@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();
}
}Partial MockingYou can use PowerMock to partially mock a method which must be non-final. How to mock and stub
PartialMockClass mockObj = PowerMockito.mockPartial(PartialMockClass.class, "methodToMock"); Mockito.when(mockObj.methodToMock()).thenReturn(123); How to verify behaviorJust use Mockito.vertify() for verification: Mockito.verify(mockObj, times(2)).methodToMock(); How to argument matchersMockito matchers are may still applied to a PowerMock mock: Mockito.verify(mockObj).methodToMockToo(Mockito.anyInt()); How to stub void static method to throw exceptionJust use Mockito semantic of setting up void method stub: Mockito.doThrow(new RuntimeException("TEST")).when(mockObj).methodToMock();A full example of partial mocking@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();
}
}
|
Sign in to add a comment