PowerMock API extension for Mockito that enables mocking of static methods, constructors, final classes and methods, private methods, and advanced testing capabilities through bytecode manipulation and custom classloading
npx @tessl/cli install tessl/maven-org-powermock--powermock-api-mockito@1.7.0PowerMock API extension for Mockito that enables mocking of static methods, constructors, final classes and methods, private methods, and advanced testing capabilities through bytecode manipulation and custom classloading. It extends Mockito's capabilities to handle traditionally "untestable" code patterns found in legacy systems.
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>import static org.powermock.api.mockito.PowerMockito.*;Individual imports:
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.mockito.PowerMockitoStubber;
import org.powermock.api.mockito.verification.PrivateMethodVerification;
import org.powermock.api.mockito.verification.ConstructorArgumentsVerification;import static org.powermock.api.mockito.PowerMockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticUtility.class, FinalClass.class})
public class PowerMockExampleTest {
@Test
public void testStaticMethodMocking() {
// Mock static methods
mockStatic(StaticUtility.class);
when(StaticUtility.getValue()).thenReturn("mocked");
// Use the mocked static method
String result = StaticUtility.getValue();
assertEquals("mocked", result);
// Verify static method was called
verifyStatic(StaticUtility.class);
StaticUtility.getValue();
}
@Test
public void testConstructorMocking() throws Exception {
// Mock constructor calls
whenNew(FileReader.class).withArguments("test.txt")
.thenThrow(new IOException("File not found"));
// Verify constructor was called
verifyNew(FileReader.class).withArguments("test.txt");
}
@Test
public void testPrivateMethodStubbing() throws Exception {
MyService service = spy(new MyService());
// Stub private method
when(service, "validateInput", "test").thenReturn(true);
// Test behavior that uses the private method
boolean result = service.processInput("test");
assertTrue(result);
// Verify private method was called
verifyPrivate(service).invoke("validateInput", "test");
}
}PowerMock extends Mockito through several key components:
This architecture enables testing of legacy code that would otherwise be untestable with standard Mockito, including static methods, final classes, constructors, and private methods.
Mock static methods on classes to control their behavior in tests, enabling isolation of code that depends on static utilities, system calls, or third-party static APIs.
static void mockStatic(Class<?> type, Class<?>... types);
static void mockStatic(Class<?> classMock, Answer defaultAnswer);
static void mockStatic(Class<?> classToMock, MockSettings mockSettings);Create mocks of final classes, final methods, and native methods that cannot be mocked with standard Mockito, while maintaining full compatibility with Mockito's API.
static <T> T mock(Class<T> type);
static <T> T mock(Class<T> classToMock, Answer defaultAnswer);
static <T> T mock(Class<T> classToMock, MockSettings mockSettings);
static <T> T spy(T object);
static <T> void spy(Class<T> type);Stub and verify private method calls on both instance and static methods, enabling comprehensive testing of internal implementation details when necessary.
static <T> OngoingStubbing<T> when(Object instance, String methodName, Object... arguments) throws Exception;
static <T> WithOrWithoutExpectedArguments<T> when(Object instance, Method method) throws Exception;
static <T> WithOrWithoutExpectedArguments<T> when(Class<?> cls, Method method) throws Exception;
static PrivateMethodVerification verifyPrivate(Object object) throws Exception;
static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode) throws Exception;Mock constructor calls to control object creation, enabling testing of code that creates objects internally without dependency injection.
static <T> ConstructorExpectationSetup<T> whenNew(Class<T> type);
static <T> WithOrWithoutExpectedArguments<T> whenNew(Constructor<T> ctor);
static <T> ConstructorExpectationSetup<T> whenNew(String fullyQualifiedName) throws Exception;
static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock);
static <T> ConstructorArgumentsVerification verifyNew(Class<?> mock, VerificationMode mode);Verify that static methods were called with expected arguments and frequencies, providing the same verification capabilities for static methods as Mockito provides for instance methods.
static <T> void verifyStatic(Class<T> mockedClass);
static <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode);Enhanced stubbing capabilities for void methods, private methods, and methods that cannot be handled by standard Mockito when() syntax.
static PowerMockitoStubber doAnswer(Answer<?> answer);
static PowerMockitoStubber doThrow(Throwable toBeThrown);
static PowerMockitoStubber doCallRealMethod();
static PowerMockitoStubber doNothing();
static PowerMockitoStubber doReturn(Object toBeReturned);
static PowerMockitoStubber doReturn(Object toBeReturned, Object... othersToBeReturned);Enhanced verification methods that work with PowerMock's advanced mocking capabilities, including verification of interactions with static methods and private methods.
static void verifyNoMoreInteractions(Object... mocks);
static void verifyZeroInteractions(Object... mocks);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 methodToExpected, 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 methodToExpected, Object... parameters) throws Exception;
}
interface PrivateMethodVerification {
void invoke(Object... arguments) throws Exception;
WithOrWithoutVerifiedArguments invoke(Method method) throws Exception;
void invoke(String methodToVerify, Object... arguments) throws Exception;
}
interface ConstructorArgumentsVerification {
void withArguments(Object argument, Object... additionalArguments) throws Exception;
void withNoArguments() throws Exception;
}
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;
}
interface WithoutExpectedArguments<T> {
OngoingStubbing<T> withNoArguments() throws Exception;
}
interface WithExpectedParameterTypes<T> {
WithExpectedArguments<T> withParameterTypes(Class<?> parameterType, Class<?>... additionalParameterTypes);
}
interface WithAnyArguments<T> {
OngoingStubbing<T> withAnyArguments() throws Exception;
}
interface PrivatelyExpectedArguments {
<T> void withArguments(Object firstArgument, Object... additionalArguments) throws Exception;
<T> void withNoArguments() throws Exception;
}
interface WithOrWithoutVerifiedArguments extends WithVerifiedArguments, WithoutVerifiedArguments {
}
interface WithVerifiedArguments {
void withArguments(Object firstArgument, Object... additionalArguments) throws Exception;
}
interface WithoutVerifiedArguments {
void withNoArguments() throws Exception;
}