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

core-mocking.mddocs/

0

# Core Mock Creation

1

2

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.

3

4

## Capabilities

5

6

### Basic Mock Creation

7

8

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

9

10

```java { .api }

11

/**

12

* Creates a mock object that supports mocking of final and native methods.

13

*

14

* @param type the type of the mock object

15

* @param methods optionally what methods to mock

16

* @return the mock object

17

*/

18

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

19

20

/**

21

* Creates a mock object that supports mocking of final and native methods.

22

*

23

* @param type the type of the mock object

24

* @return the mock object

25

*/

26

public static synchronized <T> T createMock(Class<T> type);

27

```

28

29

#### Usage Example

30

31

```java

32

import org.powermock.api.easymock.PowerMock;

33

import static org.easymock.EasyMock.expect;

34

35

// Mock a final class

36

FinalCalculator calculator = PowerMock.createMock(FinalCalculator.class);

37

expect(calculator.add(5, 3)).andReturn(8);

38

PowerMock.replay(calculator);

39

40

int result = calculator.add(5, 3);

41

assertEquals(8, result);

42

PowerMock.verify(calculator);

43

```

44

45

### Constructor-Based Mock Creation

46

47

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

48

49

```java { .api }

50

/**

51

* Creates a mock object and invokes a specific constructor.

52

*

53

* @param type the type of the mock object

54

* @param constructorArgs the constructor arguments

55

* @param methods optionally what methods to mock

56

* @return the mock object

57

*/

58

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

59

60

/**

61

* Creates a mock object and invokes a specific constructor based on argument values.

62

*

63

* @param type the type of the mock object

64

* @param constructorArguments the constructor arguments

65

* @return the mock object

66

*/

67

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

68

```

69

70

#### Usage Example

71

72

```java

73

// Mock with specific constructor

74

DatabaseConnection connection = PowerMock.createMock(DatabaseConnection.class, "localhost", 5432);

75

expect(connection.isConnected()).andReturn(true);

76

PowerMock.replay(connection);

77

78

boolean connected = connection.isConnected();

79

assertTrue(connected);

80

PowerMock.verify(connection);

81

```

82

83

### Strict Mock Creation

84

85

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

86

87

```java { .api }

88

/**

89

* Creates a strict mock object that supports mocking of final and native methods.

90

*

91

* @param type the type of the mock object

92

* @param methods optionally what methods to mock

93

* @return the mock object

94

*/

95

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

96

97

/**

98

* Creates a strict mock object that supports mocking of final and native methods.

99

*

100

* @param type the type of the mock object

101

* @return the mock object

102

*/

103

public static synchronized <T> T createStrictMock(Class<T> type);

104

105

/**

106

* Creates a strict mock with specific constructor.

107

*

108

* @param type the type of the mock object

109

* @param constructorArgs the constructor arguments

110

* @param methods optionally what methods to mock

111

* @return the mock object

112

*/

113

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

114

115

/**

116

* Creates a strict mock with constructor arguments.

117

*

118

* @param type the type of the mock object

119

* @param constructorArguments the constructor arguments

120

* @return the mock object

121

*/

122

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

123

```

124

125

#### Usage Example

126

127

```java

128

// Strict mock verifies call order

129

FileProcessor processor = PowerMock.createStrictMock(FileProcessor.class);

130

expect(processor.openFile("input.txt")).andReturn(true);

131

expect(processor.processContent()).andReturn("processed");

132

expect(processor.closeFile()).andReturn(true);

133

PowerMock.replay(processor);

134

135

// These calls must be in exactly this order

136

processor.openFile("input.txt");

137

processor.processContent();

138

processor.closeFile();

139

140

PowerMock.verify(processor);

141

```

142

143

### Nice Mock Creation

144

145

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.

146

147

```java { .api }

148

/**

149

* Creates a nice mock object that supports mocking of final and native methods.

150

*

151

* @param type the type of the mock object

152

* @param methods optionally what methods to mock

153

* @return the mock object

154

*/

155

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

156

157

/**

158

* Creates a nice mock object that supports mocking of final and native methods.

159

*

160

* @param type the type of the mock object

161

* @return the mock object

162

*/

163

public static synchronized <T> T createNiceMock(Class<T> type);

164

165

/**

166

* Creates a nice mock with specific constructor.

167

*

168

* @param type the type of the mock object

169

* @param constructorArgs the constructor arguments

170

* @param methods optionally what methods to mock

171

* @return the mock object

172

*/

173

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

174

175

/**

176

* Creates a nice mock with constructor arguments.

177

*

178

* @param type the type of the mock object

179

* @param constructorArguments the constructor arguments

180

* @return the mock object

181

*/

182

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

183

```

184

185

#### Usage Example

186

187

```java

188

// Nice mock allows unexpected calls and returns defaults

189

Logger logger = PowerMock.createNiceMock(Logger.class);

190

// Only set up the expectations you care about

191

expect(logger.isDebugEnabled()).andReturn(true);

192

PowerMock.replay(logger);

193

194

// These calls don't need explicit expectations - nice mock handles them

195

logger.info("Starting process"); // Returns void (default)

196

boolean debugEnabled = logger.isDebugEnabled(); // Returns true (expected)

197

String level = logger.getLevel(); // Returns null (default for String)

198

199

PowerMock.verify(logger);

200

```

201

202

## Integration with EasyMock

203

204

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.

205

206

```java

207

// Standard EasyMock operations work with PowerMock mocks

208

import static org.easymock.EasyMock.*;

209

210

MyService service = PowerMock.createMock(MyService.class);

211

expect(service.getData()).andReturn("test data").times(2);

212

expect(service.getStatus()).andReturn(Status.ACTIVE).once();

213

service.reset(); // void method expectation

214

expectLastCall().andThrow(new RuntimeException("Reset failed"));

215

```