or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

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

index.mddocs/

0

# PowerMock API for EasyMock

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: org.powermock:powermock-api-easymock

7

- **Package Type**: Maven

8

- **Language**: Java

9

- **Installation**: Add to Maven dependencies:

10

```xml

11

<dependency>

12

<groupId>org.powermock</groupId>

13

<artifactId>powermock-api-easymock</artifactId>

14

<version>2.0.9</version>

15

<scope>test</scope>

16

</dependency>

17

```

18

19

## Core Imports

20

21

```java

22

import org.powermock.api.easymock.PowerMock;

23

import org.powermock.api.easymock.annotation.Mock;

24

import org.powermock.api.easymock.annotation.MockStrict;

25

import org.powermock.api.easymock.annotation.MockNice;

26

import org.powermock.api.extension.listener.AnnotationEnabler;

27

import org.powermock.core.classloader.annotations.PrepareForTest;

28

import org.powermock.reflect.Whitebox;

29

```

30

31

## Basic Usage

32

33

```java

34

import org.powermock.api.easymock.PowerMock;

35

import org.powermock.core.classloader.annotations.PrepareForTest;

36

import org.junit.Test;

37

import org.junit.runner.RunWith;

38

import org.powermock.modules.junit4.PowerMockRunner;

39

40

@RunWith(PowerMockRunner.class)

41

@PrepareForTest({SystemHelper.class})

42

public class MyTest {

43

44

@Test

45

public void testStaticMethod() {

46

// Mock static method

47

PowerMock.mockStatic(SystemHelper.class);

48

expect(SystemHelper.getCurrentTime()).andReturn(123456L);

49

PowerMock.replayAll();

50

51

// Test your code that calls SystemHelper.getCurrentTime()

52

long result = MyClass.getFormattedTime();

53

54

PowerMock.verifyAll();

55

assertEquals(expected, result);

56

}

57

58

@Test

59

public void testConstructor() throws Exception {

60

// Mock constructor calls

61

DatabaseConnection mockConnection = PowerMock.createMock(DatabaseConnection.class);

62

PowerMock.expectNew(DatabaseConnection.class, "localhost", 5432)

63

.andReturn(mockConnection);

64

PowerMock.replayAll();

65

66

// Test code that creates new DatabaseConnection("localhost", 5432)

67

MyService service = new MyService();

68

service.connect();

69

70

PowerMock.verifyAll();

71

}

72

}

73

```

74

75

## Architecture

76

77

PowerMock uses a layered architecture to enable advanced mocking:

78

79

- **Custom ClassLoader**: Loads classes through PowerMock's modified bytecode instead of standard JVM loading

80

- **Bytecode Manipulation**: Modifies class bytecode at runtime to intercept method calls

81

- **Mock Strategy Layer**: Implements different mocking behaviors (default, strict, nice)

82

- **Invocation Control**: Manages expectation setup, replay, and verification phases

83

- **EasyMock Integration**: Delegates to EasyMock where possible while extending capabilities

84

85

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

86

87

## Capabilities

88

89

### Core Mock Creation

90

91

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.

92

93

```java { .api }

94

public static <T> T createMock(Class<T> type, Method... methods);

95

public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods);

96

public static <T> T createMock(Class<T> type, Object... constructorArguments);

97

public static <T> T createStrictMock(Class<T> type, Method... methods);

98

public static <T> T createNiceMock(Class<T> type, Method... methods);

99

```

100

101

[Core Mock Creation](./core-mocking.md)

102

103

### Static Method Mocking

104

105

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.

106

107

```java { .api }

108

public static void mockStatic(Class<?> type, Method... methods);

109

public static void mockStaticStrict(Class<?> type, Method... methods);

110

public static void mockStaticNice(Class<?> type, Method... methods);

111

public static void mockStaticPartial(Class<?> clazz, String... methodNames);

112

```

113

114

[Static Method Mocking](./static-mocking.md)

115

116

### Constructor and New Object Mocking

117

118

Set up expectations for constructor calls and control object instantiation. Essential for testing code that creates objects internally or depends on specific constructor behavior.

119

120

```java { .api }

121

public static <T> IExpectationSetters<T> expectNew(Class<T> type, Object... arguments) throws Exception;

122

public static <T> IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception;

123

public static <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments) throws Exception;

124

public static <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments) throws Exception;

125

public static <T> T createMockAndExpectNew(Class<T> type, Object... arguments) throws Exception;

126

```

127

128

[Constructor Mocking](./constructor-mocking.md)

129

130

### Private Method Expectations

131

132

Set up expectations for private method calls, enabling testing of internal class behavior and private method interactions without exposing implementation details.

133

134

```java { .api }

135

public static <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Object... arguments) throws Exception;

136

public static <T> IExpectationSetters<T> expectPrivate(Object instance, Method method, Object... arguments) throws Exception;

137

public static <T> IExpectationSetters<T> expectPrivate(Class<?> clazz, Method method, Object... arguments) throws Exception;

138

public static <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?> where, Object... arguments) throws Exception;

139

```

140

141

[Private Method Testing](./private-methods.md)

142

143

### Partial Mocking

144

145

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.

146

147

```java { .api }

148

public static <T> T createPartialMock(Class<T> type, String... methodNames);

149

public static <T> T createPartialMockForAllMethodsExcept(Class<T> type, String... methodNames);

150

public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception;

151

public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments);

152

```

153

154

[Partial Mocking](./partial-mocking.md)

155

156

### Mock Control and Lifecycle

157

158

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.

159

160

```java { .api }

161

public static void replayAll(Object... additionalMocks);

162

public static void verifyAll();

163

public static void resetAll(Object... additionalMocks);

164

public static void replay(Object... mocks);

165

public static void verify(Object... objects);

166

public static void reset(Object... mocks);

167

public static IExpectationSetters<Object> expectLastCall();

168

```

169

170

[Mock Control](./mock-control.md)

171

172

### Annotation-Based Configuration

173

174

Use annotations for automatic mock creation and injection, reducing boilerplate setup code and improving test readability with declarative mock configuration.

175

176

```java { .api }

177

@Mock String[] value() default "";

178

@Mock String fieldName() default "";

179

@MockStrict String[] value() default "";

180

@MockNice String[] value() default "";

181

```

182

183

[Annotations](./annotations.md)

184

185

### Reflection and Internal State Access

186

187

Access private fields, methods, and constructors using PowerMock's reflection utilities. Essential for testing internal state and private behavior without exposing implementation details.

188

189

```java { .api }

190

public static <T> T getInternalState(Object object, String fieldName);

191

public static void setInternalState(Object object, String fieldName, Object value);

192

public static <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments) throws Exception;

193

public static <T> T newInstance(Class<T> classToInstantiate);

194

```

195

196

[Reflection Utilities](./reflection.md)

197

198

## Types

199

200

```java { .api }

201

// EasyMock integration types

202

interface IExpectationSetters<T> {

203

IExpectationSetters<T> andReturn(T value);

204

IExpectationSetters<T> andThrow(Throwable throwable);

205

void andVoid();

206

IExpectationSetters<T> times(int times);

207

IExpectationSetters<T> once();

208

IExpectationSetters<T> anyTimes();

209

IExpectationSetters<T> atLeastOnce();

210

}

211

212

class ConstructorArgs {

213

public ConstructorArgs(Constructor<?> constructor, Object... initArgs);

214

public Constructor<?> getConstructor();

215

public Object[] getInitArgs();

216

}

217

218

// PowerMock specific types

219

interface FieldMatchingStrategy {

220

// Strategy for matching fields in context-based operations

221

}

222

223

// Annotation types

224

@interface Mock {

225

String[] value() default "";

226

String fieldName() default "";

227

}

228

229

@interface MockStrict {

230

String[] value() default "";

231

}

232

233

@interface MockNice {

234

String[] value() default "";

235

}

236

237

@interface PrepareForTest {

238

Class<?>[] value();

239

String[] fullyQualifiedNames() default "";

240

}

241

242

// Configuration types

243

class EasyMockConfiguration {

244

public static EasyMockConfiguration getConfiguration();

245

public boolean isTestSubjectSupported();

246

public boolean isReallyEasyMock();

247

public boolean isInjectMocksSupported();

248

}

249

```