
在junit中对返回void的方法(如`deleteobject`)进行mock时,需使用`donothing()`而非`when()`,否则会因类型不匹配导致编译错误。
在Java单元测试中,当被测类(如MyClass)直接依赖AmazonS3客户端,并在其方法(如myMethod)中调用返回void的API(例如deleteObject(DeleteObjectRequest))时,常规的when(...).thenReturn(...)语法无法使用——因为when()期望一个有返回值的方法调用,而void方法不产生可被拦截的返回值,编译器会报错:
error: when(T) cannot be applied to void
✅ 正确做法是采用 doNothing() + when() 的“stubbing void methods”模式(属于Mockito的“do-answers”系列API):
// 在@Test方法或@Before中设置 AmazonS3 s3Client = mock(AmazonS3.class); doNothing().when(s3Client).deleteObject(any(DeleteObjectRequest.class));
⚠️ 注意事项:
- doNothing() 必须配合 when(...).
() 的括号内调用形式(即when(mock).method(...)),不能写成when(mock.method(...))——后者是when()的标准用法,仅适用于非void方法; - 确保mock对象已正确注入到MyClass实例中(例如通过构造函数、Setter或字段反射);
- 若deleteObject抛出异常(如AmazonS3Exception),且你希望模拟异常行为,可改用:
doThrow(new AmazonS3Exception("Simulated error")).when(s3Client).deleteObject(any(DeleteObjectRequest.class));
? 补充:其他常见void方法Mock方式对比
| 场景 | 推荐写法 |
|------|----------|
| 无操作(静默跳过) | doNothing().when(mock).method(...) |
| 抛出异常 | doThrow(...).when(mock).method(...) |
| 执行自定义逻辑(如记录日志) | doAnswer(invocation -> { /* custom code */ }).when(mock).method(...) |
综上,只需将原错误代码:
// ❌ 编译失败 when(s3Client.deleteObject(any(DeleteObjectRequest.class))).thenReturn(null);
替换为:
// ✅ 正确且简洁 doNothing().when(s3Client).deleteObject(any(DeleteObjectRequest.class));
即可安全跳过deleteObject调用,使myMethod在测试中专注验证业务逻辑,不受真实S3交互干扰。










