0
# Verification
1
2
PowerMock's verification system provides comprehensive capabilities for verifying static method calls, private method invocations, constructor calls, and general mock interactions. This extends standard Mockito verification to cover traditionally difficult-to-test scenarios.
3
4
## Static Method Verification
5
6
### Basic Static Verification
7
8
Verify that static methods were called exactly once.
9
10
```java { .api }
11
public static <T> void verifyStatic(Class<T> mockedClass);
12
```
13
14
Verifies that static methods on the specified class were called once. This is equivalent to `verifyStatic(mockedClass, times(1))`.
15
16
**Parameters:**
17
- `mockedClass` - The class whose static methods should be verified
18
19
**Usage Example:**
20
```java
21
// Setup static mock
22
mockStatic(FileUtils.class);
23
when(FileUtils.readFile("config.txt")).thenReturn("config data");
24
25
// Execute code under test
26
String config = MyService.loadConfiguration(); // internally calls FileUtils.readFile
27
28
// Verify static method was called
29
verifyStatic(FileUtils.class);
30
FileUtils.readFile("config.txt");
31
```
32
33
### Static Verification with Mode
34
35
Verify static method calls with specific verification modes.
36
37
```java { .api }
38
public static <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode);
39
```
40
41
Verifies static method calls with custom verification modes like `times()`, `atLeast()`, `never()`, etc.
42
43
**Parameters:**
44
- `mockedClass` - The class whose static methods should be verified
45
- `verificationMode` - Verification mode (e.g., `times(2)`, `atLeastOnce()`, `never()`)
46
47
**Usage Example:**
48
```java
49
import static org.mockito.Mockito.times;
50
import static org.mockito.Mockito.atLeast;
51
import static org.mockito.Mockito.never;
52
53
// Verify called exactly 3 times
54
verifyStatic(LoggerUtils.class, times(3));
55
LoggerUtils.log(anyString());
56
57
// Verify called at least twice
58
verifyStatic(CacheUtils.class, atLeast(2));
59
CacheUtils.clear();
60
61
// Verify never called
62
verifyStatic(SecurityUtils.class, never());
63
SecurityUtils.authenticate(anyString());
64
```
65
66
## Private Method Verification
67
68
### Basic Private Method Verification
69
70
Verify private method calls on object instances.
71
72
```java { .api }
73
public static PrivateMethodVerification verifyPrivate(Object object);
74
```
75
76
Returns a verification object for verifying private method calls on the specified instance. Defaults to verifying calls happened once.
77
78
**Parameters:**
79
- `object` - The object whose private methods should be verified
80
81
**Returns:**
82
- `PrivateMethodVerification` - Verification interface for specifying method details
83
84
**Usage Example:**
85
```java
86
UserService userService = spy(new UserService());
87
88
// Execute code that should call private method
89
userService.processUser(testUser);
90
91
// Verify private method was called
92
verifyPrivate(userService).invoke("validateUser", testUser);
93
```
94
95
### Private Method Verification with Mode
96
97
Verify private method calls with specific verification modes.
98
99
```java { .api }
100
public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode);
101
```
102
103
**Usage Example:**
104
```java
105
verifyPrivate(processor, times(2)).invoke("processData", any());
106
verifyPrivate(calculator, never()).invoke("recalculate");
107
```
108
109
### Static Private Method Verification
110
111
Verify private static method calls on classes.
112
113
```java { .api }
114
public static PrivateMethodVerification verifyPrivate(Class<?> clazz) throws Exception;
115
public static PrivateMethodVerification verifyPrivate(Class<?> clazz, VerificationMode verificationMode);
116
```
117
118
**Usage Example:**
119
```java
120
// Verify static private method called once
121
verifyPrivate(UtilityClass.class).invoke("internalHelper", "param");
122
123
// Verify static private method called multiple times
124
verifyPrivate(ConfigParser.class, times(3)).invoke("parseSection", anyString());
125
```
126
127
## Private Method Verification Interface
128
129
The `PrivateMethodVerification` interface provides methods for specifying which private method to verify:
130
131
```java { .api }
132
interface PrivateMethodVerification {
133
void invoke(String methodToVerify, Object... arguments) throws Exception;
134
WithOrWithoutVerifiedArguments invoke(Method method) throws Exception;
135
@Deprecated
136
void invoke(Object... arguments) throws Exception;
137
}
138
```
139
140
### Verify by Method Name
141
142
```java
143
// Verify private method by name with specific arguments
144
verifyPrivate(service).invoke("processOrder", orderId, userId);
145
146
// Verify with argument matchers
147
verifyPrivate(validator).invoke("checkEmail", anyString());
148
```
149
150
### Verify by Method Object
151
152
```java
153
Method privateMethod = PowerMockito.method(MyClass.class, "privateHelper", String.class);
154
verifyPrivate(instance).invoke(privateMethod);
155
```
156
157
## Constructor Verification
158
159
### Basic Constructor Verification
160
161
Verify that constructors were called during test execution.
162
163
```java { .api }
164
public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock);
165
```
166
167
Verifies that the constructor of the specified class was called once.
168
169
**Usage Example:**
170
```java
171
// Setup constructor mocking
172
MyService mockService = mock(MyService.class);
173
whenNew(MyService.class).withAnyArguments().thenReturn(mockService);
174
175
// Execute code that creates instances
176
MyController controller = new MyController(); // internally creates MyService
177
178
// Verify constructor was called
179
verifyNew(MyService.class);
180
```
181
182
### Constructor Verification with Mode
183
184
```java { .api }
185
public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock, VerificationMode mode);
186
```
187
188
**Usage Example:**
189
```java
190
verifyNew(DatabaseConnection.class, times(2));
191
verifyNew(FileProcessor.class, never());
192
```
193
194
## Constructor Arguments Verification Interface
195
196
The `ConstructorArgumentsVerification` interface allows verification of specific constructor arguments:
197
198
```java { .api }
199
interface ConstructorArgumentsVerification {
200
void withArguments(Object argument, Object... additionalArguments) throws Exception;
201
void withNoArguments() throws Exception;
202
}
203
```
204
205
**Usage Examples:**
206
```java
207
// Verify constructor called with specific arguments
208
verifyNew(UserService.class).withArguments("admin", "password");
209
210
// Verify no-arg constructor
211
verifyNew(DefaultLogger.class).withNoArguments();
212
```
213
214
## General Verification
215
216
### Verify No More Interactions
217
218
Ensure no unverified interactions occurred on mocks.
219
220
```java { .api }
221
public static void verifyNoMoreInteractions(Object... mocks);
222
```
223
224
Checks that all interactions with the specified mocks have been verified. Useful for ensuring complete test coverage.
225
226
**Usage Example:**
227
```java
228
UserService userMock = mock(UserService.class);
229
OrderService orderMock = mock(OrderService.class);
230
231
// Setup and execute test...
232
when(userMock.getUser(1L)).thenReturn(testUser);
233
userMock.getUser(1L);
234
235
// Verify expected interactions
236
verify(userMock).getUser(1L);
237
238
// Ensure no other interactions occurred
239
verifyNoMoreInteractions(userMock, orderMock);
240
```
241
242
### Verify Zero Interactions
243
244
Verify that no interactions occurred on the specified mocks.
245
246
```java { .api }
247
public static void verifyZeroInteractions(Object... mocks);
248
```
249
250
**Usage Example:**
251
```java
252
EmailService emailMock = mock(EmailService.class);
253
SmsService smsMock = mock(SmsService.class);
254
255
// Execute test that shouldn't trigger notifications
256
userService.updateProfile(user);
257
258
// Verify no notification services were called
259
verifyZeroInteractions(emailMock, smsMock);
260
```
261
262
## Advanced Verification Patterns
263
264
### Combining Verification Types
265
266
```java
267
// Verify static methods
268
verifyStatic(SecurityUtils.class);
269
SecurityUtils.checkPermission("admin");
270
271
// Verify private methods
272
verifyPrivate(service).invoke("auditAction", "update");
273
274
// Verify constructors
275
verifyNew(AuditLog.class).withArguments("update", userId);
276
277
// Ensure no other interactions
278
verifyNoMoreInteractions(mockDependency);
279
```
280
281
### Verification with Argument Matchers
282
283
```java
284
import static org.mockito.ArgumentMatchers.*;
285
286
// Static method verification with matchers
287
verifyStatic(LoggerUtils.class, times(2));
288
LoggerUtils.log(eq("INFO"), contains("user"));
289
290
// Private method verification with matchers
291
verifyPrivate(processor).invoke("processItem", any(Item.class), eq(true));
292
293
// Constructor verification with matchers
294
verifyNew(Connection.class).withArguments(startsWith("jdbc:"), anyString());
295
```
296
297
### Error Handling in Verification
298
299
```java
300
try {
301
verifyPrivate(service).invoke("nonExistentMethod");
302
} catch (Exception e) {
303
// Handle reflection errors when method doesn't exist
304
fail("Private method not found: " + e.getMessage());
305
}
306
```