or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-stubbing.mdconstructor-mocking.mdindex.mdobject-mocking.mdprivate-methods.mdstatic-mocking.mdstatic-verification.mdverification-extensions.md

static-verification.mddocs/

0

# Static Method Verification

1

2

PowerMock provides comprehensive verification capabilities for static method calls, enabling you to verify that static methods were called with expected arguments and frequencies. This complements static mocking by providing the same verification guarantees for static methods as Mockito provides for instance methods.

3

4

## Capabilities

5

6

### Basic Static Verification

7

8

Verify that static methods on a specific class were called exactly once.

9

10

```java { .api }

11

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

12

```

13

14

**Parameters:**

15

- `mockedClass` - The class whose static method calls should be verified

16

17

**Usage Example:**

18

```java

19

@Test

20

@PrepareForTest(Logger.class)

21

public void testBasicStaticVerification() {

22

mockStatic(Logger.class);

23

24

// Code under test that calls static method

25

MyService service = new MyService();

26

service.performOperation();

27

28

// Verify static method was called once

29

verifyStatic(Logger.class);

30

Logger.log("Operation completed");

31

}

32

```

33

34

### Static Verification with Mode

35

36

Verify that static methods were called with specific verification modes (times, atLeast, never, etc.).

37

38

```java { .api }

39

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

40

```

41

42

**Parameters:**

43

- `mockedClass` - The class whose static method calls should be verified

44

- `verificationMode` - The verification mode (times(n), atLeast(n), atMost(n), never(), etc.)

45

46

**Usage Example:**

47

```java

48

@Test

49

@PrepareForTest(SecurityAudit.class)

50

public void testStaticVerificationWithMode() {

51

mockStatic(SecurityAudit.class);

52

53

UserService service = new UserService();

54

service.loginUser("user1");

55

service.loginUser("user2");

56

service.loginUser("user3");

57

58

// Verify audit log was called exactly 3 times

59

verifyStatic(SecurityAudit.class, times(3));

60

SecurityAudit.logLogin(anyString());

61

62

// Verify security check was called at least once

63

verifyStatic(SecurityAudit.class, atLeast(1));

64

SecurityAudit.performSecurityCheck();

65

}

66

```

67

68

## Verification Modes

69

70

PowerMock supports all standard Mockito verification modes:

71

72

```java

73

times(n) // Exactly n times

74

atLeast(n) // At least n times

75

atMost(n) // At most n times

76

never() // Never called

77

atLeastOnce() // At least once (same as atLeast(1))

78

only() // Only this method was called on the mock

79

```

80

81

## Common Patterns

82

83

### Verifying Static Utility Calls

84

85

```java

86

@Test

87

@PrepareForTest(StringUtils.class)

88

public void testUtilityVerification() {

89

mockStatic(StringUtils.class);

90

when(StringUtils.isEmpty(anyString())).thenReturn(false);

91

92

DataValidator validator = new DataValidator();

93

validator.validateInput("test input");

94

95

// Verify utility method was used for validation

96

verifyStatic(StringUtils.class);

97

StringUtils.isEmpty("test input");

98

99

// Verify trimming was also performed

100

verifyStatic(StringUtils.class);

101

StringUtils.trim("test input");

102

}

103

```

104

105

### Verifying Static Calls with Complex Arguments

106

107

```java

108

@Test

109

@PrepareForTest(DatabaseLogger.class)

110

public void testComplexArgumentVerification() {

111

mockStatic(DatabaseLogger.class);

112

113

OrderProcessor processor = new OrderProcessor();

114

Order order = new Order("12345", new BigDecimal("99.99"));

115

processor.processOrder(order);

116

117

// Verify static method was called with specific object

118

verifyStatic(DatabaseLogger.class);

119

DatabaseLogger.logTransaction(eq(order.getId()), eq(order.getAmount()));

120

121

// Verify using matchers for complex objects

122

verifyStatic(DatabaseLogger.class);

123

DatabaseLogger.logTransaction(matches("\\d{5}"), any(BigDecimal.class));

124

}

125

```

126

127

### Verifying Multiple Static Method Calls

128

129

```java

130

@Test

131

@PrepareForTest({ConfigManager.class, CacheManager.class})

132

public void testMultipleStaticVerifications() {

133

mockStatic(ConfigManager.class);

134

mockStatic(CacheManager.class);

135

136

when(ConfigManager.getProperty("cache.enabled")).thenReturn("true");

137

138

ApplicationService service = new ApplicationService();

139

service.initialize();

140

141

// Verify configuration was checked

142

verifyStatic(ConfigManager.class);

143

ConfigManager.getProperty("cache.enabled");

144

145

// Verify cache was initialized based on config

146

verifyStatic(CacheManager.class);

147

CacheManager.initialize();

148

149

// Verify cache was not disabled

150

verifyStatic(CacheManager.class, never());

151

CacheManager.disable();

152

}

153

```

154

155

### Verifying Static Call Order

156

157

```java

158

@Test

159

@PrepareForTest(SystemLogger.class)

160

public void testStaticCallOrder() {

161

mockStatic(SystemLogger.class);

162

InOrder inOrder = inOrder(SystemLogger.class);

163

164

StartupService service = new StartupService();

165

service.startup();

166

167

// Verify static methods were called in specific order

168

verifyStatic(SystemLogger.class, inOrder);

169

SystemLogger.log("Starting application");

170

171

verifyStatic(SystemLogger.class, inOrder);

172

SystemLogger.log("Loading configuration");

173

174

verifyStatic(SystemLogger.class, inOrder);

175

SystemLogger.log("Application started");

176

}

177

```

178

179

### Verifying No Static Interactions

180

181

```java

182

@Test

183

@PrepareForTest(ExternalService.class)

184

public void testNoStaticInteractions() {

185

mockStatic(ExternalService.class);

186

187

LocalService service = new LocalService();

188

service.performLocalOperation();

189

190

// Verify external service was never called for local operations

191

verifyStatic(ExternalService.class, never());

192

ExternalService.callExternalAPI(anyString());

193

194

// Alternative: verify no interactions at all

195

verifyNoMoreInteractions(ExternalService.class);

196

}

197

```

198

199

### Conditional Static Verification

200

201

```java

202

@Test

203

@PrepareForTest(NotificationService.class)

204

public void testConditionalStaticVerification() {

205

mockStatic(NotificationService.class);

206

207

EmailService emailService = new EmailService();

208

209

// Test with notifications enabled

210

emailService.sendEmail("user@example.com", "subject", "body", true);

211

verifyStatic(NotificationService.class);

212

NotificationService.sendNotification(anyString());

213

214

// Reset static mock

215

reset(NotificationService.class);

216

217

// Test with notifications disabled

218

emailService.sendEmail("user@example.com", "subject", "body", false);

219

verifyStatic(NotificationService.class, never());

220

NotificationService.sendNotification(anyString());

221

}

222

```

223

224

### Verifying Static Method Arguments with Matchers

225

226

```java

227

@Test

228

@PrepareForTest(MetricsCollector.class)

229

public void testArgumentMatchers() {

230

mockStatic(ReflectionHelper.class);

231

232

ReportGenerator generator = new ReportGenerator();

233

generator.generateReport("sales", LocalDate.now());

234

235

// Verify with argument matchers

236

verifyStatic(MetricsCollector.class);

237

MetricsCollector.recordMetric(

238

eq("report.generated"),

239

argThat(map -> map.containsKey("reportType") && map.get("reportType").equals("sales"))

240

);

241

242

// Verify timestamp argument is recent

243

verifyStatic(MetricsCollector.class);

244

MetricsCollector.recordTimestamp(

245

argThat(timestamp -> timestamp.isAfter(LocalDateTime.now().minusMinutes(1)))

246

);

247

}

248

```

249

250

## Integration with Standard Mockito Verification

251

252

Static verification works seamlessly with standard Mockito verification patterns:

253

254

```java

255

@Test

256

@PrepareForTest(AuditLogger.class)

257

public void testMixedVerification() {

258

mockStatic(AuditLogger.class);

259

UserRepository mockRepo = mock(UserRepository.class);

260

261

UserService service = new UserService(mockRepo);

262

service.createUser("john.doe", "john@example.com");

263

264

// Verify instance method call

265

verify(mockRepo).save(any(User.class));

266

267

// Verify static method call

268

verifyStatic(AuditLogger.class);

269

AuditLogger.logUserCreation("john.doe");

270

271

// Combined verification with timing

272

verify(mockRepo, timeout(1000)).save(any(User.class));

273

verifyStatic(AuditLogger.class, timeout(1000));

274

AuditLogger.logUserCreation("john.doe");

275

}

276

```

277

278

## Deprecated Static Verification Methods

279

280

**⚠️ These methods are deprecated and will be removed in PowerMock 2.0:**

281

282

```java { .api }

283

static void verifyStatic();

284

static void verifyStatic(VerificationMode verificationMode);

285

```

286

287

These methods do not specify which class to verify, making them ambiguous in tests with multiple static mocks. Use the class-specific variants instead:

288

289

```java

290

// Deprecated - avoid

291

verifyStatic();

292

FileUtils.readFile("test.txt");

293

294

// Recommended - explicit class specification

295

verifyStatic(FileUtils.class);

296

FileUtils.readFile("test.txt");

297

```

298

299

## Requirements

300

301

- Static methods must be mocked with `mockStatic()` before verification

302

- Classes with static methods must be specified in `@PrepareForTest` annotation

303

- Test must use `@RunWith(PowerMockRunner.class)` or equivalent

304

- Static verification must occur after the code under test has executed

305

- Method signatures in verification must exactly match the static method calls