# 断言失败错误：期望抛出 java.lang.RuntimeException，但实际抛出了 java.lang.NullPointerException

- **ID:** `java/junit-assert-throws-wrong-type`
- **领域:** java
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

assertThrows 期望特定的异常类型，但抛出了不同的异常。

## 版本兼容性

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

## 解决方案

1. **Use assertThrows with correct exception type** (95% 成功率)
   ```
   assertThrows(NullPointerException.class, () -> {
    // code that throws NPE
});
   ```
2. **Use assertThrowsAny for multiple exception types** (80% 成功率)
   ```
   assertThrowsAny(RuntimeException.class, () -> {
    // code
});
   ```

## 无效尝试

- **Catching the actual exception and ignoring** — Test passes incorrectly; real exception type mismatch is hidden. (60% 失败率)
- **Changing expected type to Exception** — Too broad; may hide other issues. (50% 失败率)
