# java.lang.AssertionError: 期望值:<X> 但实际值:<Y>

- **ID:** `java/assertion-error`
- **领域:** java
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

AssertionError 在 Java 断言（assert 语句）失败时抛出，通常用于调试或测试以验证不变量，或当测试框架（如 JUnit）检测到断言失败时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| JUnit 4.13 | active | — | — |
| JUnit 5.9 | active | — | — |

## 解决方案

1. ```
   Fix the code logic to ensure the assertion condition is true. For example, if the assertion is 'assert x == 10', correct the variable x to equal 10 before the assertion.
   ```
2. ```
   Use a proper testing framework like JUnit with assertEquals(expected, actual) instead of assert statements, which provides better error messages and is always enabled.
   ```

## 无效尝试

- **** — AssertionError indicates a programming error; ignoring it allows the bug to persist and may cause unpredictable behavior later. (80% 失败率)
- **** — Assertions are a development-time tool; disabling them does not fix the underlying issue and can lead to silent failures. (90% 失败率)
