java
runtime_error
ai_generated
true
参数捕获器异常:ArgumentCaptor 的 capture() 方法从未被调用
org.mockito.exceptions.base.MockitoException: ArgumentCaptor's capture() method was never invoked
ID: java/mockito-argumentcaptor-not-invoked
80%修复率
87%置信度
0证据数
2024-07-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 8+ | active | — | — | — |
根因分析
使用了 ArgumentCaptor,但未验证方法是否实际使用捕获的参数被调用。
English
ArgumentCaptor used without verifying that the method was actually called with the captured argument.
解决方案
-
95% 成功率 Use verify with captor
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(mock).method(captor.capture()); assertEquals("expected", captor.getValue()); -
90% 成功率 Use @Captor annotation
@Captor ArgumentCaptor<String> captor; verify(mock).method(captor.capture());
无效尝试
常见但无效的做法:
-
Calling captor.getValue() without verify
90% 失败
Capture only happens during verification, so value is null.
-
Using captor.capture() in when() instead of verify()
70% 失败
capture() is for verification, not stubbing.