|
MockitoUsage
Using PowerMock with Mockito 1.7Note: 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();
}
}
|
I couldn't use PowerMockito?.verifyStatic(Static.class) (with a class) parameter since it requires a VerificationMode?. To verify just use it without a class as parameter, e.g PowerMockito?.verifyStatic().
The API must have changed while this wiki page is (kinda) outdated.
This is for Mockito 1.7 and older versions of PowerMock? (1.2.5). You should probably look at http://code.google.com/p/powermock/wiki/MockitoUsage13.
Is it me or PowerMockito.mockPartial doesn't exist (or doesn't exist anymore)? grep -r mockPartial on the source code of 1.3.8, 1.3.9 and 1.4 doesn't return anything.
You looking at the old documentation. The new documentation is located under "MockitoUsage13".
How can i setup Powermock to mock two different static classes.
if i do @prepareForTest(Static.class); @prepareForTest(AnotherStatic?.class);
It complains about a duplicate annotation
@PrepareForTest?({staticclass1.class, staticclass2.class})
Just pass an array of classes
In which distribution can I find mockStaticPartial and mockPartial methods? Tried all distributions, PowerMockito? class doesn't have those methods in any of the packages listed on the download page. Thanks.