验证断言是否已在 Assertj 中调用
Verify that assertions have been called in Assertj
我正在阅读使用 Assertj 验证结果的测试 类。
偶尔,我发现了一个没有断言的 assertThat。
assertThat(object.getField());
是否可以在开发周期的某处识别这些 类?我的第一个猜测是使用自定义声纳规则。虽然我不明白我应该如何定义这个方法后面应该有一个断言(一个返回 void 的方法?)。
如the AssertJ FAQ所述:
Static code analysis tools like SpotBugs/FindBugs/ErrorProne can now detect such problems thanks to the CheckReturnValue
annotation introduced in 2.5+ / 3.5+ and improved in 2.7+ / 3.7+.
事实上,SpotBugs 很容易发现这个问题,因为我刚刚使用 AssertJ 3.9.0、Java 8 和 SpotBugs 3.1.1 进行了测试:
因此,如果您在静态分析工具中没有看到此警告,可能是您禁用了对使用 @CheckReturnValue
.
注释的方法中的 return 值的检查。
SonarJava 的规则 S2970 "Assertions should be complete" 可以检测 assertThat
而无需对 AssertJ、Fest 和 Truth 进行断言。
我正在阅读使用 Assertj 验证结果的测试 类。 偶尔,我发现了一个没有断言的 assertThat。
assertThat(object.getField());
是否可以在开发周期的某处识别这些 类?我的第一个猜测是使用自定义声纳规则。虽然我不明白我应该如何定义这个方法后面应该有一个断言(一个返回 void 的方法?)。
如the AssertJ FAQ所述:
Static code analysis tools like SpotBugs/FindBugs/ErrorProne can now detect such problems thanks to the
CheckReturnValue
annotation introduced in 2.5+ / 3.5+ and improved in 2.7+ / 3.7+.
事实上,SpotBugs 很容易发现这个问题,因为我刚刚使用 AssertJ 3.9.0、Java 8 和 SpotBugs 3.1.1 进行了测试:
因此,如果您在静态分析工具中没有看到此警告,可能是您禁用了对使用 @CheckReturnValue
.
SonarJava 的规则 S2970 "Assertions should be complete" 可以检测 assertThat
而无需对 AssertJ、Fest 和 Truth 进行断言。