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

- **ID:** `java/mockito-strict-stubbing-unnecessary`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 8+ | active | — | — |

## 解决方案

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

## 无效尝试

- **Removing all stubs blindly** — May cause test to fail if stubs were actually needed but not triggered. (50% 失败率)
- **Adding @MockitoSettings(strictness = Strictness.LENIENT) globally** — Hides real issues, may lead to false positives. (40% 失败率)
