Files
2026Technology-Competition/data/demo-pmd/reports/AssertStmtTest-review.md
T

55 lines
3.5 KiB
Markdown

# 代码审查报告
**文件:** `data\demo-pmd\src\com\demo\errorprone\extra\AssertStmtTest.java`
**语言:** java
**耗时:** 112.2s
**分析工具:** pmd
---
总计: 12 | 错误: 4 | 警告: 7 | 建议: 1
静态分析 · 10 个问题
- 🔴 `pmd:WrongTestAnnotation` L10
org.junit.Test 注解来自 JUnit 4,而当前代码库使用的是 JUnit Jupiter。
建议: 将 import org.junit.Test; 改为 import org.junit.jupiter.api.Test;,并确保使用 JUnit Jupiter 依赖。
- 🔴 `pmd:WrongTestAnnotation` L19
org.junit.Test 注解来自 JUnit 4,而当前代码库使用的是 JUnit Jupiter。
建议: 改用在 JUnit Jupiter 下用 assertThrows 断言异常:将 @Test(expected = ArithmeticException.class) 改为 @Test,并在方法内使用 Assertions.assertThrows 包裹除零逻辑。
- 🔴 `pmd:SystemPrintln` L27
使用了 System.out/err 输出。
建议: 建议使用日志框架(如 java.util.logging、SLF4J)替代 System.out 输出。
- 🟡 `pmd:AtLeastOneConstructor` L9
每个类应至少声明一个构造函数
建议: 为 AssertStmtTest 类显式添加一个构造函数,例如:public AssertStmtTest() {}
- 🟡 `pmd:UnitTestShouldIncludeAssert` L11
该单元测试应包含 assert() 或 fail()。
建议: 在测试方法中增加断言,例如使用 org.junit.jupiter.api.Assertions.assertEquals(1, x) 替代 assert x == 1。
- 🟡 `pmd:LocalVariableCouldBeFinal` L12
局部变量 'x' 可以声明为 final。
建议: 将 int x = 1; 改为 final int x = 1;,表示变量初始化后不再变化。
- 🟡 `pmd:AssertStatementInTest` L13
测试代码中不应使用 assert 语句。
建议: 不要使用 Java 关键字 assert,改用 JUnit 断言方法,例如 assertEquals(1, x)。
- 🟡 `pmd:AtLeastOneConstructor` L18
每个类应至少声明一个构造函数
建议: 为 JUnitExpectedTest 类显式添加一个构造函数,例如:JUnitExpectedTest() {}。
- 🟡 `pmd:PublicMemberInNonPublicType` L20
公共成员 'testExpected' 声明在非公共类型中。
建议: 由于一个 .java 文件只能有一个 public 类,此处应将测试方法改为包私有可见性:将 public void testExpected() 改为 void testExpected()。
- 🟡 `pmd:UnusedLocalVariable` L21
避免未使用的局部变量 'x'。
建议: 删除未使用的变量,或直接在断言 lambda 中使用该表达式,例如:assertThrows(ArithmeticException.class, () -> { int zero = 0; if (1 / zero == 0) {} })。
AI 审查 · 2 条建议
- 🔴 [AI] [bug] `constant-division-by-zero` L21
**常量除零导致编译失败**
第21行的 `int x = 1 / 0;` 中,1 和 0 都是整数常量表达式,Java 编译器会在编译期直接计算并报错 'division by zero',导致整个源文件无法编译。PMD 未检查编译期错误,因此需要手动修复。
建议: 将除数改为非常量表达式,例如使用变量 `int zero = 0; int x = 1 / zero;`,或者改用 `assertThrows(ArithmeticException.class, () -> { int zero = 0; if (1 / zero == 0) {} })`,确保除零发生在运行时。
- 🔵 [AI] [design] `no-private-constructor-for-main-class` L25
**包含 main 方法的类未提供私有构造函数**
AssertMain 是一个仅包含静态 main 方法的入口类,不应被实例化。建议添加私有构造函数,避免外部创建实例,同时满足构造相关设计约束。
建议: 在 AssertMain 类中添加私有构造函数:`private AssertMain() { throw new AssertionError(); }`,并将类声明为 final(可选)。