java
data_error
ai_generated
true
断言失败错误:期望抛出 java.lang.RuntimeException,但实际抛出了 java.lang.NullPointerException
org.opentest4j.AssertionFailedError: Expected java.lang.RuntimeException to be thrown, but java.lang.NullPointerException was thrown
ID: java/junit-assert-throws-wrong-type
80%修复率
82%置信度
0证据数
2024-12-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 8+ | active | — | — | — |
根因分析
assertThrows 期望特定的异常类型,但抛出了不同的异常。
English
assertThrows expects a specific exception type, but a different exception is thrown.
解决方案
-
95% 成功率 Use assertThrows with correct exception type
assertThrows(NullPointerException.class, () -> { // code that throws NPE }); -
80% 成功率 Use assertThrowsAny for multiple exception types
assertThrowsAny(RuntimeException.class, () -> { // code });
无效尝试
常见但无效的做法:
-
Catching the actual exception and ignoring
60% 失败
Test passes incorrectly; real exception type mismatch is hidden.
-
Changing expected type to Exception
50% 失败
Too broad; may hide other issues.