java runtime_error ai_generated true

org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected in test class

ID: java/mockito-strict-stubbing-unnecessary

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8+ active

Root Cause

Stubs defined with when() but never used during the test execution.

generic

中文

使用 when() 定义的桩代码在测试执行过程中从未被使用。

Workarounds

  1. 85% success Use lenient() for specific stubs
    lenient().when(mock.method()).thenReturn(value);
  2. 90% success Remove unused stubs
    // Remove the stub line that is never used
    // when(mock.method()).thenReturn(value);

Dead Ends

Common approaches that don't work:

  1. Removing all stubs blindly 50% fail

    May cause test to fail if stubs were actually needed but not triggered.

  2. Adding @MockitoSettings(strictness = Strictness.LENIENT) globally 40% fail

    Hides real issues, may lead to false positives.