java runtime_error ai_generated true

org.mockito.exceptions.base.MockitoException: ArgumentCaptor's capture() method was never invoked

ID: java/mockito-argumentcaptor-not-invoked

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8+ active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Calling captor.getValue() without verify 90% fail

    Capture only happens during verification, so value is null.

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

    capture() is for verification, not stubbing.