CtrlK
BlogDocsLog inGet started
Tessl Logo

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.

Pending
Overview
Eval results
Files

core-mocking.mddocs/

Core Mock Creation

PowerMock's core mock creation capabilities provide the foundation for mocking objects that would be impossible to mock with standard frameworks. This includes final classes, final methods, and objects with specific constructor requirements.

Capabilities

Basic Mock Creation

Create mock objects with optional method filtering. Methods not specified will retain their original behavior.

/**
 * Creates a mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static synchronized <T> T createMock(Class<T> type, Method... methods);

/**
 * Creates a mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @return the mock object
 */
public static synchronized <T> T createMock(Class<T> type);

Usage Example

import org.powermock.api.easymock.PowerMock;
import static org.easymock.EasyMock.expect;

// Mock a final class
FinalCalculator calculator = PowerMock.createMock(FinalCalculator.class);
expect(calculator.add(5, 3)).andReturn(8);
PowerMock.replay(calculator);

int result = calculator.add(5, 3);
assertEquals(8, result);
PowerMock.verify(calculator);

Constructor-Based Mock Creation

Create mocks that invoke specific constructors, useful when the class requires initialization parameters or has multiple constructors.

/**
 * Creates a mock object and invokes a specific constructor.
 * 
 * @param type the type of the mock object
 * @param constructorArgs the constructor arguments
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods);

/**
 * Creates a mock object and invokes a specific constructor based on argument values.
 * 
 * @param type the type of the mock object
 * @param constructorArguments the constructor arguments
 * @return the mock object
 */
public static <T> T createMock(Class<T> type, Object... constructorArguments);

Usage Example

// Mock with specific constructor
DatabaseConnection connection = PowerMock.createMock(DatabaseConnection.class, "localhost", 5432);
expect(connection.isConnected()).andReturn(true);
PowerMock.replay(connection);

boolean connected = connection.isConnected();
assertTrue(connected);
PowerMock.verify(connection);

Strict Mock Creation

Create strict mocks that verify method call order. Useful when the sequence of method calls is important for correctness.

/**
 * Creates a strict mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static synchronized <T> T createStrictMock(Class<T> type, Method... methods);

/**
 * Creates a strict mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @return the mock object
 */
public static synchronized <T> T createStrictMock(Class<T> type);

/**
 * Creates a strict mock with specific constructor.
 * 
 * @param type the type of the mock object
 * @param constructorArgs the constructor arguments
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static <T> T createStrictMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods);

/**
 * Creates a strict mock with constructor arguments.
 * 
 * @param type the type of the mock object
 * @param constructorArguments the constructor arguments
 * @return the mock object
 */
public static <T> T createStrictMock(Class<T> type, Object... constructorArguments);

Usage Example

// Strict mock verifies call order
FileProcessor processor = PowerMock.createStrictMock(FileProcessor.class);
expect(processor.openFile("input.txt")).andReturn(true);
expect(processor.processContent()).andReturn("processed");
expect(processor.closeFile()).andReturn(true);
PowerMock.replay(processor);

// These calls must be in exactly this order
processor.openFile("input.txt");
processor.processContent();
processor.closeFile();

PowerMock.verify(processor);

Nice Mock Creation

Create nice mocks that provide default return values for unexpected method calls. Useful for lenient testing where not all method interactions need to be explicitly defined.

/**
 * Creates a nice mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static synchronized <T> T createNiceMock(Class<T> type, Method... methods);

/**
 * Creates a nice mock object that supports mocking of final and native methods.
 * 
 * @param type the type of the mock object
 * @return the mock object
 */
public static synchronized <T> T createNiceMock(Class<T> type);

/**
 * Creates a nice mock with specific constructor.
 * 
 * @param type the type of the mock object
 * @param constructorArgs the constructor arguments
 * @param methods optionally what methods to mock
 * @return the mock object
 */
public static <T> T createNiceMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods);

/**
 * Creates a nice mock with constructor arguments.
 * 
 * @param type the type of the mock object
 * @param constructorArguments the constructor arguments
 * @return the mock object
 */
public static <T> T createNiceMock(Class<T> type, Object... constructorArguments);

Usage Example

// Nice mock allows unexpected calls and returns defaults
Logger logger = PowerMock.createNiceMock(Logger.class);
// Only set up the expectations you care about
expect(logger.isDebugEnabled()).andReturn(true);
PowerMock.replay(logger);

// These calls don't need explicit expectations - nice mock handles them
logger.info("Starting process");    // Returns void (default)
boolean debugEnabled = logger.isDebugEnabled();  // Returns true (expected)
String level = logger.getLevel();   // Returns null (default for String)

PowerMock.verify(logger);

Integration with EasyMock

PowerMock delegates to EasyMock for standard mocking operations while extending capabilities for previously unmockable constructs. All EasyMock expectation and verification methods work with PowerMock-created mocks.

// Standard EasyMock operations work with PowerMock mocks
import static org.easymock.EasyMock.*;

MyService service = PowerMock.createMock(MyService.class);
expect(service.getData()).andReturn("test data").times(2);
expect(service.getStatus()).andReturn(Status.ACTIVE).once();
service.reset(); // void method expectation
expectLastCall().andThrow(new RuntimeException("Reset failed"));

Install with Tessl CLI

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

docs

annotations.md

constructor-mocking.md

core-mocking.md

index.md

mock-control.md

partial-mocking.md

private-methods.md

reflection.md

static-mocking.md

tile.json