PowerMock API extension for EasyMock providing advanced mocking capabilities for static methods, constructors, final classes, and private methods through bytecode manipulation.
npx @tessl/cli install tessl/maven-org-powermock--powermock-api-easymock@2.0.0PowerMock API for EasyMock extends the standard EasyMock framework with advanced mocking capabilities that enable testing of previously untestable code. It provides mocking for static methods, constructors, final classes and methods, private methods, and more through bytecode manipulation and custom classloading while maintaining full compatibility with the standard EasyMock API.
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>import org.powermock.api.easymock.PowerMock;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.api.easymock.annotation.MockStrict;
import org.powermock.api.easymock.annotation.MockNice;
import org.powermock.api.extension.listener.AnnotationEnabler;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.reflect.Whitebox;import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SystemHelper.class})
public class MyTest {
@Test
public void testStaticMethod() {
// Mock static method
PowerMock.mockStatic(SystemHelper.class);
expect(SystemHelper.getCurrentTime()).andReturn(123456L);
PowerMock.replayAll();
// Test your code that calls SystemHelper.getCurrentTime()
long result = MyClass.getFormattedTime();
PowerMock.verifyAll();
assertEquals(expected, result);
}
@Test
public void testConstructor() throws Exception {
// Mock constructor calls
DatabaseConnection mockConnection = PowerMock.createMock(DatabaseConnection.class);
PowerMock.expectNew(DatabaseConnection.class, "localhost", 5432)
.andReturn(mockConnection);
PowerMock.replayAll();
// Test code that creates new DatabaseConnection("localhost", 5432)
MyService service = new MyService();
service.connect();
PowerMock.verifyAll();
}
}PowerMock uses a layered architecture to enable advanced mocking:
This design allows PowerMock to mock typically unmockable constructs without requiring changes to production code or build infrastructure.
Create mock objects with support for final classes, methods, and constructors. Provides standard, strict, and nice mock variants with optional method filtering and constructor argument specification.
public static <T> T createMock(Class<T> type, Method... methods);
public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods);
public static <T> T createMock(Class<T> type, Object... constructorArguments);
public static <T> T createStrictMock(Class<T> type, Method... methods);
public static <T> T createNiceMock(Class<T> type, Method... methods);Enable mocking of static methods with support for method filtering and different mock behaviors. Allows testing code that depends on static utility methods, system calls, or third-party static APIs.
public static void mockStatic(Class<?> type, Method... methods);
public static void mockStaticStrict(Class<?> type, Method... methods);
public static void mockStaticNice(Class<?> type, Method... methods);
public static void mockStaticPartial(Class<?> clazz, String... methodNames);Set up expectations for constructor calls and control object instantiation. Essential for testing code that creates objects internally or depends on specific constructor behavior.
public static <T> IExpectationSetters<T> expectNew(Class<T> type, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments) throws Exception;
public static <T> T createMockAndExpectNew(Class<T> type, Object... arguments) throws Exception;Set up expectations for private method calls, enabling testing of internal class behavior and private method interactions without exposing implementation details.
public static <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectPrivate(Object instance, Method method, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectPrivate(Class<?> clazz, Method method, Object... arguments) throws Exception;
public static <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?> where, Object... arguments) throws Exception;Create partial mocks that mock only specific methods while leaving others intact. Useful for testing classes where you want to mock some methods but retain real behavior for others.
public static <T> T createPartialMock(Class<T> type, String... methodNames);
public static <T> T createPartialMockForAllMethodsExcept(Class<T> type, String... methodNames);
public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception;
public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments);Control mock behavior through replay/verify cycles and manage mock state. Provides bulk operations for managing multiple mocks and integration with EasyMock's control mechanisms.
public static void replayAll(Object... additionalMocks);
public static void verifyAll();
public static void resetAll(Object... additionalMocks);
public static void replay(Object... mocks);
public static void verify(Object... objects);
public static void reset(Object... mocks);
public static IExpectationSetters<Object> expectLastCall();Use annotations for automatic mock creation and injection, reducing boilerplate setup code and improving test readability with declarative mock configuration.
@Mock String[] value() default "";
@Mock String fieldName() default "";
@MockStrict String[] value() default "";
@MockNice String[] value() default "";Access private fields, methods, and constructors using PowerMock's reflection utilities. Essential for testing internal state and private behavior without exposing implementation details.
public static <T> T getInternalState(Object object, String fieldName);
public static void setInternalState(Object object, String fieldName, Object value);
public static <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments) throws Exception;
public static <T> T newInstance(Class<T> classToInstantiate);// EasyMock integration types
interface IExpectationSetters<T> {
IExpectationSetters<T> andReturn(T value);
IExpectationSetters<T> andThrow(Throwable throwable);
void andVoid();
IExpectationSetters<T> times(int times);
IExpectationSetters<T> once();
IExpectationSetters<T> anyTimes();
IExpectationSetters<T> atLeastOnce();
}
class ConstructorArgs {
public ConstructorArgs(Constructor<?> constructor, Object... initArgs);
public Constructor<?> getConstructor();
public Object[] getInitArgs();
}
// PowerMock specific types
interface FieldMatchingStrategy {
// Strategy for matching fields in context-based operations
}
// Annotation types
@interface Mock {
String[] value() default "";
String fieldName() default "";
}
@interface MockStrict {
String[] value() default "";
}
@interface MockNice {
String[] value() default "";
}
@interface PrepareForTest {
Class<?>[] value();
String[] fullyQualifiedNames() default "";
}
// Configuration types
class EasyMockConfiguration {
public static EasyMockConfiguration getConfiguration();
public boolean isTestSubjectSupported();
public boolean isReallyEasyMock();
public boolean isInjectMocksSupported();
}