我正在用 Java 编写测试用例。我想集成 FindBugs Maven 插件来提高测试类中的代码质量。我已将 FindBugs Maven Plugin 添加到测试模块的 POM 文件中。我可以成功创建 FindBugs XML 文档,但即使 FindBugs 在我的代码中发现了一些错误,构建也会成功。
有人可以指导我吗?
我的配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
<includeTests>true</includeTests>
<failOnError>true</failOnError>
</configuration>
<executions>
<execution>
<id>analyze-compile</id>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
最佳答案
FindBugs Maven 插件的行为有所不同,具体取决于是否 src/main/java
是否为空。如果它不存在(模块仅用于测试并且仅包含 src/test/java
)构建不会失败:
[INFO] --- findbugs-maven-plugin:3.0.3:findbugs (findbugs) @ findbugs-fail-build ---
[INFO] Fork Value is true
[java] Warnings generated: 1
[INFO] Done FindBugs Analysis....
[INFO]
[INFO] <<< findbugs-maven-plugin:3.0.3:check (findbugs) < :findbugs @ findbugs-fail-build <<<
[INFO]
[INFO] --- findbugs-maven-plugin:3.0.3:check (findbugs) @ findbugs-fail-build ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
当模块中有一些生产代码(src/main/java
不为空)时,构建失败:
[INFO] --- findbugs-maven-plugin:3.0.3:findbugs (findbugs) @ findbugs-fail-build ---
[INFO] Fork Value is true
[java] Warnings generated: 1
[INFO] Done FindBugs Analysis....
[INFO]
[INFO] <<< findbugs-maven-plugin:3.0.3:check (findbugs) < :findbugs @ findbugs-fail-build <<<
[INFO]
[INFO] --- findbugs-maven-plugin:3.0.3:check (findbugs) @ findbugs-fail-build ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[INFO] Private method io.github.mkordas.ClassWithFindBugsViolationInTest.unusedPrivateMethod() is never called [io.github.mkordas.ClassWithFindBugsViolationInTest] At ClassWithFindBugsViolationInTest.java:[line 5] UPM_UNCALLED_PRIVATE_METHOD
[INFO]
To see bug detail using the Findbugs GUI, use the following command "mvn findbugs:gui"
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
这是 FindBugs Maven 插件中的错误。我已将其报告为 gleclaire/findbugs-maven-plugin#33 。在 FindbugsViolationCheckMojo有一个 if 检查“主”类是否存在,如果不存在,插件假设没有分析任何内容,并且不会调用导致构建失败的代码。
有两种解决方法:
- 添加
<classFilesDirectory>${project.build.testOutputDirectory}</classFilesDirectory>
配置 - 将任何代码添加到
src/main/java
关于wso2 - 集成测试模块中的 FindBugs Maven 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796044/