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

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-07-22首次发现

版本兼容性

版本状态引入弃用备注
8+ active

根因分析

使用了 ArgumentCaptor,但未验证方法是否实际使用捕获的参数被调用。

English

ArgumentCaptor used without verifying that the method was actually called with the captured argument.

generic

解决方案

  1. 95% 成功率 Use verify with captor
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(mock).method(captor.capture());
    assertEquals("expected", captor.getValue());
  2. 90% 成功率 Use @Captor annotation
    @Captor
    ArgumentCaptor<String> captor;
    
    verify(mock).method(captor.capture());

无效尝试

常见但无效的做法:

  1. Calling captor.getValue() without verify 90% 失败

    Capture only happens during verification, so value is null.

  2. Using captor.capture() in when() instead of verify() 70% 失败

    capture() is for verification, not stubbing.