or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

annotations.mdconstructor-mocking.mdcore-mocking.mdindex.mdmock-control.mdpartial-mocking.mdprivate-methods.mdreflection.mdstatic-mocking.md
tile.json

tessl/maven-org-powermock--powermock-api-easymock

PowerMock API extension for EasyMock providing advanced mocking capabilities for static methods, constructors, final classes, and private methods through bytecode manipulation.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.powermock/powermock-api-easymock@2.0.x

To install, run

npx @tessl/cli install tessl/maven-org-powermock--powermock-api-easymock@2.0.0

index.mddocs/

PowerMock API for EasyMock

PowerMock 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.

Package Information

  • Package Name: org.powermock:powermock-api-easymock
  • Package Type: Maven
  • Language: Java
  • Installation: Add to Maven dependencies:
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-easymock</artifactId>
      <version>2.0.9</version>
      <scope>test</scope>
    </dependency>

Core Imports

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;

Basic Usage

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

Architecture

PowerMock uses a layered architecture to enable advanced mocking:

  • Custom ClassLoader: Loads classes through PowerMock's modified bytecode instead of standard JVM loading
  • Bytecode Manipulation: Modifies class bytecode at runtime to intercept method calls
  • Mock Strategy Layer: Implements different mocking behaviors (default, strict, nice)
  • Invocation Control: Manages expectation setup, replay, and verification phases
  • EasyMock Integration: Delegates to EasyMock where possible while extending capabilities

This design allows PowerMock to mock typically unmockable constructs without requiring changes to production code or build infrastructure.

Capabilities

Core Mock Creation

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

Core Mock Creation

Static Method Mocking

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

Static Method Mocking

Constructor and New Object Mocking

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;

Constructor Mocking

Private Method Expectations

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;

Private Method Testing

Partial Mocking

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

Partial Mocking

Mock Control and Lifecycle

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();

Mock Control

Annotation-Based Configuration

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 "";

Annotations

Reflection and Internal State Access

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

Reflection Utilities

Types

// 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();
}