我正在编写用Java编写测试用例。我想集成FindBugs Maven插件来提高我在测试类中的代码质量。我已将FindBugs Maven插件添加到测试模块的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>
答案 0 :(得分:2)
根据src/main/java
是否为空,FindBugs Maven插件的行为会有所不同。如果它不存在(模块是仅测试且仅包含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插件中的 bug 。我已将其报告为gleclaire/findbugs-maven-plugin#33。在FindbugsViolationCheckMojo中,有一个if检查'main'类是否存在,如果不存在,插件假定没有分析任何内容,并且没有调用构建失败的代码。
有两种解决方法:
<classFilesDirectory>${project.build.testOutputDirectory}</classFilesDirectory>
配置src/main/java