java runtime_error ai_generated true

不必要的桩代码异常:在测试类中检测到不必要的桩代码

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

ID: java/mockito-strict-stubbing-unnecessary

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

版本兼容性

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

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Removing all stubs blindly 50% 失败

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

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

    Hides real issues, may lead to false positives.