56 lines
3.6 KiB
Markdown
56 lines
3.6 KiB
Markdown
# 代码审查报告
|
||
|
||
**文件:** `data\demo-pmd\src\com\demo\errorprone\extra\JUnit5ArgOrderTest.java`
|
||
**语言:** java
|
||
**耗时:** 94.8s
|
||
**分析工具:** pmd
|
||
|
||
---
|
||
|
||
总计: 12 | 错误: 2 | 警告: 8 | 建议: 2
|
||
|
||
静态分析 · 9 个问题
|
||
|
||
- 🔴 `pmd:SystemPrintln` L24
|
||
使用了 System.out / System.err。
|
||
建议: 移除该输出语句,或改用日志框架(如 java.util.logging、SLF4J)进行记录。
|
||
- 🟡 `pmd:AtLeastOneConstructor` L10
|
||
每个类都应声明至少一个构造函数。
|
||
建议: 为类添加一个显式的构造函数,例如:`public JUnit5ArgOrderTest() {}`,或确保存在其他构造函数。
|
||
- 🟡 `pmd:JUnitJupiterTestShouldBePackagePrivate` L10
|
||
JUnit 5 测试类应为包私有(不要使用 public 修饰符)。
|
||
建议: 将 `public class JUnit5ArgOrderTest` 改为 `class JUnit5ArgOrderTest`,使测试类为包私有。
|
||
- 🟡 `pmd:JUnitJupiterTestShouldBePackagePrivate` L12
|
||
JUnit 5 测试方法应为包私有(不要使用 public 修饰符)。
|
||
建议: 将 `public void testX()` 改为 `void testX()`。
|
||
- 🟡 `pmd:LocalVariableCouldBeFinal` L13
|
||
局部变量 `actual` 可以声明为 final。
|
||
建议: 在变量声明处添加 `final` 修饰符:`final String actual = next("foo");`。
|
||
- 🟡 `pmd:AssertEqualsArgumentOrder` L14
|
||
此 assertEquals() 调用的 expected 和 actual 参数顺序颠倒了。
|
||
建议: 将期望值作为第一个参数,实际值作为第二个参数:`assertEquals("bar", actual);`。
|
||
- 🟡 `pmd:UnitTestAssertionsShouldIncludeMessage` L14
|
||
单元测试断言应包含一个描述性消息。
|
||
建议: 为断言添加消息参数:`assertEquals(expected, actual, "消息内容");`,例如 `assertEquals("bar", actual, "next() 的返回值应等于 bar");`。
|
||
- 🟡 `pmd:CommentDefaultAccessModifier` L17
|
||
方法 `next(String)` 缺少默认访问修饰符(包私有)的注释。
|
||
建议: 在方法前添加注释 `/* package */`,或者显式声明访问修饰符(如 `private`)。
|
||
- 🟡 `pmd:MethodArgumentCouldBeFinal` L17
|
||
参数 `s` 未被赋值,可以声明为 final。
|
||
建议: 在参数类型前添加 `final`:`String next(final String s)`。
|
||
|
||
AI 审查 · 3 条建议
|
||
|
||
- 🔴 [AI] [bug] `wrong-test-expectation` L14
|
||
**测试断言期望值与实际输入不匹配**
|
||
`next("foo")` 返回 `"foo"`(因为 `next` 直接返回参数),但断言期望值为 `"bar"`。即使把 assertEquals 参数顺序调整正确,`assertEquals("bar", actual)` 仍会失败,因为 `actual` 是 `"foo"`。请检查测试意图:要么将期望值改为 `"foo"`,要么让 `next` 方法产生 `"bar"` 作为结果。
|
||
建议: 如果 `next` 被设计为恒等函数,应将断言改为 `assertEquals("foo", actual)`;否则修改 `next` 的实现以返回预期值。
|
||
- 🔵 [AI] [design] `method-can-be-static` L17
|
||
**辅助方法可声明为 static**
|
||
`next(String)` 不访问任何实例字段或实例方法,应声明为 `static`,以更清晰地表达其无状态性,并避免实例持有。
|
||
建议: 将 `String next(String s)` 改为 `static String next(String s)`;调用处 `next("foo")` 在实例方法内仍可调用静态方法,无需修改。
|
||
- 🔵 [AI] [design] `top-level-class-in-test-file` L22
|
||
**测试文件中不应包含无关的顶层类**
|
||
`Junit5Main` 是一个独立的顶层类,包含 `main` 方法,却与测试类 `JUnit5ArgOrderTest` 放在同一个源文件中。这样会降低可读性,并可能导致构建工具扫描到非测试类。建议将 `Junit5Main` 移到单独的 `Junit5Main.java` 文件中。
|
||
建议: 将 `Junit5Main` 类移到独立文件,或将其从测试源集中移除。
|