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
—
PowerMock enables mocking of static methods, allowing you to control the behavior of static calls in your tests. This is essential for testing code that depends on static utilities, system calls, or third-party static APIs without requiring major refactoring.
Enable static mocking for one or more classes, making all static methods on those classes mockable.
static void mockStatic(Class<?> type, Class<?>... types);Parameters:
type - The primary class to enable static mocking fortypes - Additional classes to enable static mocking for (optional)Usage Example:
@Test
@PrepareForTest({FileUtils.class, StringUtils.class})
public void testMultipleStaticMocks() {
mockStatic(FileUtils.class, StringUtils.class);
when(FileUtils.readFileToString(any(File.class))).thenReturn("file content");
when(StringUtils.isEmpty(anyString())).thenReturn(false);
// Test code that uses both static methods
String result = MyService.processFile(new File("test.txt"));
assertEquals("processed: file content", result);
}Create static mocks with a custom default answer strategy for unstubbed method calls.
static void mockStatic(Class<?> classMock, Answer defaultAnswer);Parameters:
classMock - The class to enable static mocking fordefaultAnswer - Default answer strategy for unstubbed methodsUsage Example:
@Test
@PrepareForTest(SystemUtils.class)
public void testStaticMockWithCustomAnswer() {
mockStatic(SystemUtils.class, RETURNS_SMART_NULLS);
// Only stub specific method, others return smart nulls
when(SystemUtils.getJavaVersion()).thenReturn("1.8.0");
String version = SystemUtils.getJavaVersion(); // Returns "1.8.0"
String os = SystemUtils.getOSName(); // Returns smart null
}Create static mocks with advanced MockSettings configuration for specialized testing scenarios.
static void mockStatic(Class<?> classToMock, MockSettings mockSettings);Parameters:
classToMock - The class to enable static mocking formockSettings - Advanced mock configuration settingsUsage Example:
@Test
@PrepareForTest(DatabaseUtils.class)
public void testStaticMockWithSettings() {
mockStatic(DatabaseUtils.class,
withSettings()
.name("DatabaseUtilsMock")
.defaultAnswer(RETURNS_DEEP_STUBS));
when(DatabaseUtils.getConnection().createStatement().executeQuery(anyString()))
.thenReturn(mockResultSet);
ResultSet result = DatabaseUtils.getConnection()
.createStatement()
.executeQuery("SELECT * FROM users");
assertEquals(mockResultSet, result);
}@Test
@PrepareForTest(System.class)
public void testSystemDependency() {
mockStatic(System.class);
when(System.currentTimeMillis()).thenReturn(1234567890L);
TimestampService service = new TimestampService();
long timestamp = service.getCurrentTimestamp();
assertEquals(1234567890L, timestamp);
}@Test
@PrepareForTest(HttpClients.class)
public void testHttpClientUsage() {
mockStatic(HttpClients.class);
CloseableHttpClient mockClient = mock(CloseableHttpClient.class);
when(HttpClients.createDefault()).thenReturn(mockClient);
MyHttpService service = new MyHttpService();
service.makeRequest("http://example.com");
verify(mockClient).execute(any(HttpGet.class));
}@Test
@PrepareForTest(MathUtils.class)
public void testPartialStaticMocking() {
mockStatic(MathUtils.class);
// Stub only specific methods, let others use real implementation
when(MathUtils.complexCalculation(anyDouble())).thenReturn(42.0);
doCallRealMethod().when(MathUtils.class);
MathUtils.simpleAddition(2, 3); // This will call real method
double result = MathUtils.complexCalculation(100.0); // Returns 42.0
assertEquals(42.0, result, 0.001);
}@PrepareForTest annotation@RunWith(PowerMockRunner.class) or equivalentInstall with Tessl CLI
npx tessl i tessl/maven-org-powermock--powermock-api-mockito