CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-powermock--powermock-api-mockito

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

Pending
Overview
Eval results
Files

static-mocking.mddocs/

Static Method Mocking

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.

Capabilities

Basic Static Mocking

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 for
  • types - 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);
}

Static Mocking with Custom Answer

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 for
  • defaultAnswer - Default answer strategy for unstubbed methods

Usage 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
}

Static Mocking with Settings

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 for
  • mockSettings - Advanced mock configuration settings

Usage 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);
}

Common Patterns

Testing System Dependencies

@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);
}

Mocking Third-Party Static APIs

@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));
}

Partial Static Mocking

@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);
}

Requirements

  • Classes must be specified in @PrepareForTest annotation
  • Test must use @RunWith(PowerMockRunner.class) or equivalent
  • Static mocking must be enabled before any stubbing
  • All static method calls on mocked classes go through PowerMock's interception

Install with Tessl CLI

npx tessl i tessl/maven-org-powermock--powermock-api-mockito

docs

advanced-stubbing.md

constructor-mocking.md

index.md

object-mocking.md

private-methods.md

static-mocking.md

static-verification.md

verification-extensions.md

tile.json