# 回答异常：回答抛出异常：java.lang.RuntimeException: test exception

- **ID:** `java/mockito-answer-throws-exception`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

自定义 Answer 实现 在测试调用期间抛出异常。

## 版本兼容性

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

## 解决方案

1. **Fix Answer implementation to handle edge cases** (90% 成功率)
   ```
   Answer<String> answer = invocation -> {
    if (invocation.getArgument(0) == null) {
        return "default";
    }
    return invocation.getArgument(0).toString();
};
   ```
2. **Use thenAnswer with lambda** (85% 成功率)
   ```
   when(mock.method(any())).thenAnswer(invocation -> {
    // safe logic
    return "result";
});
   ```

## 无效尝试

- **Catching exception in test and ignoring** — Hides the real issue; test may pass incorrectly. (60% 失败率)
- **Using thenThrow instead of custom Answer** — May not match the intended behavior if Answer has complex logic. (40% 失败率)
