集成测试模块中的 FindBugs Maven 插件

FindBugs Maven Plugin in integration test module

我正在 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>

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 插件中的 bug。我将其报告为 gleclaire/findbugs-maven-plugin#33. In FindbugsViolationCheckMojo 有一个 if 检查 'main' 类 是否存在,如果不存在,插件假定没有分析任何内容并且不会调用构建失败的代码。

有两种解决方法:

  • 添加 <classFilesDirectory>${project.build.testOutputDirectory}</classFilesDirectory> 配置
  • 将任何代码添加到 src/main/java