Mocking multiple static classes in the same test method with PowerMock, while verifying their interaction counts and preserving the original behavior for non-mocked methods, can be achieved through the following steps. I'll provide a detailed example to illustrate this.
Dependencies (assuming you're using Maven)
Ensure you have the necessary PowerMock dependencies in your pom.xml:
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Example Static Classes
Let's say we have two static utility classes, Logger and NetworkUtils, and we want to mock them in the same test:
// Logger.java
public class Logger {
public static void log(String message) {
System.out.println("Logging: " + message);
}
public static String getLogLevel() {
return "INFO"; // Original behavior we want to keep
}
}
// NetworkUtils.java
public class NetworkUtils {
public static boolean isConnected() {
// Original implementation, let's assume it returns false for demo
return false;
}
public static void sendRequest(String data) {
System.out.println("Sending request with data: " + data);
}
}
Test Class
Now, let's create a test class that mocks these static classes, verifies their interaction counts, and maintains the original behavior for non-mocked methods:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Logger.class, NetworkUtils.class}) // Prepare both classes for mocking
public class MultiMockTest {
@Test
public void testMultiMock() {
// Mock static classes
PowerMockito.mockStatic(Logger.class);
PowerMockito.mockStatic(NetworkUtils.class);
// Define mock behavior for specific methods
PowerMockito.when(Logger.log(anyString())).thenReturn(null); // Mock log to do nothing
PowerMockito.when(NetworkUtils.isConnected()).thenReturn(true); // Mock isConnected to return true
PowerMockito.doNothing().when(NetworkUtils.class, "sendRequest", anyString()); // Mock sendRequest to do nothing
// **Keep original behavior for these methods (no mocking defined)**
// - Logger.getLogLevel()
// Call methods under test
SystemUnderTest systemUnderTest = new SystemUnderTest();
systemUnderTest.doSomething();
// Verify interaction counts
PowerMockito.verifyStatic(Logger.class, times(2)); // Assuming log is called twice in doSomething()
Logger.log(anyString());
PowerMockito.verifyStatic(NetworkUtils.class, times(1)); // isConnected is called once
NetworkUtils.isConnected();
PowerMockito.verifyStatic(NetworkUtils.class, times(1)); // sendRequest is called once
NetworkUtils.sendRequest(anyString());
// Verify original behavior is maintained
assertEquals("INFO", Logger.getLogLevel()); // Should still return "INFO"
}
}
// Example class that uses the static methods
class SystemUnderTest {
public void doSomething() {
if (NetworkUtils.isConnected()) {
Logger.log("Connected, sending request...");
NetworkUtils.sendRequest("Hello, Server!");
Logger.log("Request sent.");
}
String currentLogLevel = Logger.getLogLevel(); // Uses the original method
System.out.println("Current Log Level: " + currentLogLevel);
}
}
Key Points:
- @PrepareForTest: Include all static classes you wish to mock in this annotation.
- Mocking Specific Methods: Only define mock behavior for methods you want to alter. Others will retain their original behavior.
- Verification: Use
PowerMockito.verifyStatic() with times() to check the interaction count for each mocked method.
- Original Behavior: Methods without explicit mock definitions will behave as originally implemented.