PowerMock API for Mockito 2.+ that enables advanced mocking of static methods, constructors, final classes and private methods through bytecode manipulation.
npx @tessl/cli install tessl/maven-org-powermock--powermock-api-mockito2@2.0.0PowerMock API for Mockito 2.+ extends standard Mockito capabilities with advanced mocking features for static methods, constructors, final classes and methods, private methods, and static initializer removal. This library bridges the gap between Mockito 2.x and PowerMock's advanced capabilities through bytecode manipulation and custom classloading.
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
</dependency>import static org.powermock.api.mockito.PowerMockito.*;For specific functionality:
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.mockito.verification.PrivateMethodVerification;
import org.powermock.api.mockito.verification.ConstructorArgumentsVerification;For member introspection:
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberMatcher.methods;
import static org.powermock.api.support.membermodification.MemberMatcher.methodsDeclaredIn;import static org.powermock.api.mockito.PowerMockito.*;
import static org.mockito.Mockito.*;
public class BasicUsageExample {
// Mock static methods
public void mockStaticMethods() {
mockStatic(StaticUtility.class);
when(StaticUtility.staticMethod()).thenReturn("mocked");
String result = StaticUtility.staticMethod();
assertEquals("mocked", result);
verifyStatic(StaticUtility.class);
StaticUtility.staticMethod();
}
// Mock constructors
public void mockConstructors() throws Exception {
SomeClass mockInstance = mock(SomeClass.class);
whenNew(SomeClass.class).withNoArguments().thenReturn(mockInstance);
SomeClass instance = new SomeClass();
assertSame(mockInstance, instance);
verifyNew(SomeClass.class).withNoArguments();
}
// Mock private methods
public void mockPrivateMethods() throws Exception {
MyClass spy = spy(new MyClass());
when(spy, "privateMethod", anyString()).thenReturn("mocked");
String result = spy.callPrivateMethod("test");
assertEquals("mocked", result);
verifyPrivate(spy).invoke("privateMethod", "test");
}
}PowerMock API for Mockito2 is built around several key components:
Advanced mock creation capabilities including support for final classes, static method mocking, and spying on objects that are normally not "spyable".
// Basic mock creation
public static <T> T mock(Class<T> type);
public static <T> T mock(Class<T> classToMock, Answer defaultAnswer);
public static <T> T mock(Class<T> classToMock, MockSettings mockSettings);
// Static mocking
public static void mockStatic(Class<?> type, Class<?>... types);
public static void mockStatic(Class<?> classMock, Answer defaultAnswer);
public static void mockStatic(Class<?> classToMock, MockSettings mockSettings);
// Spying
public static <T> T spy(T object);
public static <T> void spy(Class<T> type);Mock Creation and Static Mocking
Comprehensive verification system for static method calls, private method invocations, constructor calls, and general interaction verification.
// Static method verification
public static <T> void verifyStatic(Class<T> mockedClass);
public static <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode);
// Private method verification
public static PrivateMethodVerification verifyPrivate(Object object);
public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode);
// Constructor verification
public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock);
public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock, VerificationMode mode);
// General verification
public static void verifyNoMoreInteractions(Object... mocks);
public static void verifyZeroInteractions(Object... mocks);Fluent API for setting up expectations on method calls, constructor invocations, and private method behavior with comprehensive argument matching.
// Method expectation setup
public static <T> OngoingStubbing<T> when(Object instance, String methodName, Object... arguments);
public static <T> WithOrWithoutExpectedArguments<T> when(Object instance, Method method);
public static <T> WithOrWithoutExpectedArguments<T> when(Class<?> cls, Method method);
// Constructor expectation setup
public static <T> WithOrWithoutExpectedArguments<T> whenNew(Constructor<T> ctor);
public static <T> ConstructorExpectationSetup<T> whenNew(Class<T> type);
public static <T> ConstructorExpectationSetup<T> whenNew(String fullyQualifiedName);Advanced stubbing capabilities for void methods, private methods, and complex stubbing scenarios using the PowerMockito stubber interface.
// Stubbing methods
public static PowerMockitoStubber doAnswer(Answer<?> answer);
public static PowerMockitoStubber doThrow(Throwable toBeThrown);
public static PowerMockitoStubber doCallRealMethod();
public static PowerMockitoStubber doNothing();
public static PowerMockitoStubber doReturn(Object toBeReturned);
public static PowerMockitoStubber doReturn(Object toBeReturned, Object... othersToBeReturned);Powerful member introspection capabilities for finding methods by name, parameter types, or class hierarchy traversal.
// Method introspection by name and parameter types
public static Method method(Class<?> declaringClass, String methodName, Class<?>... parameterTypes);
public static Method method(Class<?> declaringClass, Class<?>... parameterTypes);
// Method array retrieval by names
public static Method[] methods(Class<?> clazz, String methodName, String... additionalMethodNames);
// Class hierarchy method discovery
public static Method[] methodsDeclaredIn(Class<?> cls, Class<?>... additionalClasses);// Expectation setup interfaces
interface ConstructorExpectationSetup<T> extends WithOrWithoutExpectedArguments<T>,
WithExpectedParameterTypes<T>, WithAnyArguments<T> {}
interface WithOrWithoutExpectedArguments<T> extends WithExpectedArguments<T>,
WithoutExpectedArguments<T> {}
interface WithExpectedArguments<T> {
OngoingStubbing<T> withArguments(Object firstArgument, Object... additionalArguments) throws Exception;
}
// Verification interfaces
interface PrivateMethodVerification {
void invoke(String methodToVerify, Object... arguments) throws Exception;
WithOrWithoutVerifiedArguments invoke(Method method) throws Exception;
}
interface ConstructorArgumentsVerification {
// Constructor argument verification methods
}
// Stubbing interface
interface PowerMockitoStubber extends Stubber {
void when(Class<?> classMock);
<T> PrivatelyExpectedArguments when(T mock, Method method) throws Exception;
<T> void when(T mock, Object... arguments) throws Exception;
<T> void when(T mock, String methodToExpect, Object... arguments) throws Exception;
<T> PrivatelyExpectedArguments when(Class<T> classMock, Method method) throws Exception;
<T> void when(Class<T> classMock, Object... arguments) throws Exception;
<T> void when(Class<T> classMock, String methodToExpect, Object... arguments) throws Exception;
}class ClassNotPreparedException extends RuntimeException {
public ClassNotPreparedException(String message);
}class Slf4jMockPolicy implements PowerMockPolicy {
public void applyClassLoadingPolicy(MockPolicyClassLoadingSettings settings);
public void applyInterceptionPolicy(MockPolicyInterceptionSettings settings);
}