or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

expectations.mdindex.mdmock-creation.mdstubbing.mdverification.md

index.mddocs/

0

# PowerMock API for Mockito2

1

2

PowerMock API for Mockito 2.+ extends standard Mockito capabilities with advanced mocking features for static methods, constructors, final classes and methods, private methods, and static initializer removal. This library bridges the gap between Mockito 2.x and PowerMock's advanced capabilities through bytecode manipulation and custom classloading.

3

4

## Package Information

5

6

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

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**:

10

```xml

11

<dependency>

12

<groupId>org.powermock</groupId>

13

<artifactId>powermock-api-mockito2</artifactId>

14

<version>2.0.9</version>

15

</dependency>

16

```

17

18

## Core Imports

19

20

```java

21

import static org.powermock.api.mockito.PowerMockito.*;

22

```

23

24

For specific functionality:

25

26

```java

27

import org.powermock.api.mockito.PowerMockito;

28

import org.powermock.api.mockito.verification.PrivateMethodVerification;

29

import org.powermock.api.mockito.verification.ConstructorArgumentsVerification;

30

```

31

32

For member introspection:

33

34

```java

35

import static org.powermock.api.support.membermodification.MemberMatcher.method;

36

import static org.powermock.api.support.membermodification.MemberMatcher.methods;

37

import static org.powermock.api.support.membermodification.MemberMatcher.methodsDeclaredIn;

38

```

39

40

## Basic Usage

41

42

```java

43

import static org.powermock.api.mockito.PowerMockito.*;

44

import static org.mockito.Mockito.*;

45

46

public class BasicUsageExample {

47

48

// Mock static methods

49

public void mockStaticMethods() {

50

mockStatic(StaticUtility.class);

51

when(StaticUtility.staticMethod()).thenReturn("mocked");

52

53

String result = StaticUtility.staticMethod();

54

assertEquals("mocked", result);

55

56

verifyStatic(StaticUtility.class);

57

StaticUtility.staticMethod();

58

}

59

60

// Mock constructors

61

public void mockConstructors() throws Exception {

62

SomeClass mockInstance = mock(SomeClass.class);

63

whenNew(SomeClass.class).withNoArguments().thenReturn(mockInstance);

64

65

SomeClass instance = new SomeClass();

66

assertSame(mockInstance, instance);

67

68

verifyNew(SomeClass.class).withNoArguments();

69

}

70

71

// Mock private methods

72

public void mockPrivateMethods() throws Exception {

73

MyClass spy = spy(new MyClass());

74

when(spy, "privateMethod", anyString()).thenReturn("mocked");

75

76

String result = spy.callPrivateMethod("test");

77

assertEquals("mocked", result);

78

79

verifyPrivate(spy).invoke("privateMethod", "test");

80

}

81

}

82

```

83

84

## Architecture

85

86

PowerMock API for Mockito2 is built around several key components:

87

88

- **PowerMockito Class**: Main entry point providing static methods for all PowerMock functionality

89

- **Expectation Framework**: Fluent interfaces for setting up mock expectations on constructors and private methods

90

- **Verification System**: Comprehensive verification capabilities for static methods, private methods, and constructors

91

- **Mock Creation Engine**: Advanced mock creation supporting final classes, static methods, and complex scenarios

92

- **Integration Layer**: Seamless integration with Mockito 2.x APIs and behavior

93

94

## Capabilities

95

96

### Mock Creation and Static Mocking

97

98

Advanced mock creation capabilities including support for final classes, static method mocking, and spying on objects that are normally not "spyable".

99

100

```java { .api }

101

// Basic mock creation

102

public static <T> T mock(Class<T> type);

103

public static <T> T mock(Class<T> classToMock, Answer defaultAnswer);

104

public static <T> T mock(Class<T> classToMock, MockSettings mockSettings);

105

106

// Static mocking

107

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

108

public static void mockStatic(Class<?> classMock, Answer defaultAnswer);

109

public static void mockStatic(Class<?> classToMock, MockSettings mockSettings);

110

111

// Spying

112

public static <T> T spy(T object);

113

public static <T> void spy(Class<T> type);

114

```

115

116

[Mock Creation and Static Mocking](./mock-creation.md)

117

118

### Verification

119

120

Comprehensive verification system for static method calls, private method invocations, constructor calls, and general interaction verification.

121

122

```java { .api }

123

// Static method verification

124

public static <T> void verifyStatic(Class<T> mockedClass);

125

public static <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode);

126

127

// Private method verification

128

public static PrivateMethodVerification verifyPrivate(Object object);

129

public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode);

130

131

// Constructor verification

132

public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock);

133

public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock, VerificationMode mode);

134

135

// General verification

136

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

137

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

138

```

139

140

[Verification](./verification.md)

141

142

### Expectation Setup

143

144

Fluent API for setting up expectations on method calls, constructor invocations, and private method behavior with comprehensive argument matching.

145

146

```java { .api }

147

// Method expectation setup

148

public static <T> OngoingStubbing<T> when(Object instance, String methodName, Object... arguments);

149

public static <T> WithOrWithoutExpectedArguments<T> when(Object instance, Method method);

150

public static <T> WithOrWithoutExpectedArguments<T> when(Class<?> cls, Method method);

151

152

// Constructor expectation setup

153

public static <T> WithOrWithoutExpectedArguments<T> whenNew(Constructor<T> ctor);

154

public static <T> ConstructorExpectationSetup<T> whenNew(Class<T> type);

155

public static <T> ConstructorExpectationSetup<T> whenNew(String fullyQualifiedName);

156

```

157

158

[Expectation Setup](./expectations.md)

159

160

### Stubbing

161

162

Advanced stubbing capabilities for void methods, private methods, and complex stubbing scenarios using the PowerMockito stubber interface.

163

164

```java { .api }

165

// Stubbing methods

166

public static PowerMockitoStubber doAnswer(Answer<?> answer);

167

public static PowerMockitoStubber doThrow(Throwable toBeThrown);

168

public static PowerMockitoStubber doCallRealMethod();

169

public static PowerMockitoStubber doNothing();

170

public static PowerMockitoStubber doReturn(Object toBeReturned);

171

public static PowerMockitoStubber doReturn(Object toBeReturned, Object... othersToBeReturned);

172

```

173

174

[Stubbing](./stubbing.md)

175

176

### Member Introspection

177

178

Powerful member introspection capabilities for finding methods by name, parameter types, or class hierarchy traversal.

179

180

```java { .api }

181

// Method introspection by name and parameter types

182

public static Method method(Class<?> declaringClass, String methodName, Class<?>... parameterTypes);

183

public static Method method(Class<?> declaringClass, Class<?>... parameterTypes);

184

185

// Method array retrieval by names

186

public static Method[] methods(Class<?> clazz, String methodName, String... additionalMethodNames);

187

188

// Class hierarchy method discovery

189

public static Method[] methodsDeclaredIn(Class<?> cls, Class<?>... additionalClasses);

190

```

191

192

## Types

193

194

### Core Interfaces

195

196

```java { .api }

197

// Expectation setup interfaces

198

interface ConstructorExpectationSetup<T> extends WithOrWithoutExpectedArguments<T>,

199

WithExpectedParameterTypes<T>, WithAnyArguments<T> {}

200

201

interface WithOrWithoutExpectedArguments<T> extends WithExpectedArguments<T>,

202

WithoutExpectedArguments<T> {}

203

204

interface WithExpectedArguments<T> {

205

OngoingStubbing<T> withArguments(Object firstArgument, Object... additionalArguments) throws Exception;

206

}

207

208

// Verification interfaces

209

interface PrivateMethodVerification {

210

void invoke(String methodToVerify, Object... arguments) throws Exception;

211

WithOrWithoutVerifiedArguments invoke(Method method) throws Exception;

212

}

213

214

interface ConstructorArgumentsVerification {

215

// Constructor argument verification methods

216

}

217

218

// Stubbing interface

219

interface PowerMockitoStubber extends Stubber {

220

void when(Class<?> classMock);

221

<T> PrivatelyExpectedArguments when(T mock, Method method) throws Exception;

222

<T> void when(T mock, Object... arguments) throws Exception;

223

<T> void when(T mock, String methodToExpect, Object... arguments) throws Exception;

224

<T> PrivatelyExpectedArguments when(Class<T> classMock, Method method) throws Exception;

225

<T> void when(Class<T> classMock, Object... arguments) throws Exception;

226

<T> void when(Class<T> classMock, String methodToExpect, Object... arguments) throws Exception;

227

}

228

```

229

230

### Exception Types

231

232

```java { .api }

233

class ClassNotPreparedException extends RuntimeException {

234

public ClassNotPreparedException(String message);

235

}

236

```

237

238

### Mock Policy Types

239

240

```java { .api }

241

class Slf4jMockPolicy implements PowerMockPolicy {

242

public void applyClassLoadingPolicy(MockPolicyClassLoadingSettings settings);

243

public void applyInterceptionPolicy(MockPolicyInterceptionSettings settings);

244

}

245

```