satkunam rajeenthini
satkunam rajeenthini

Reputation: 151

FindBugs Maven Plugin in integration test module

I am working on writing test cases in Java. I would like to integrate FindBugs Maven Plugin to enhance my code quality in test classes. I have added FindBugs Maven Plugin to POM file of the test module. I can successfully create FindBugs XML document but build succeeds even FindBugs found some bugs in my code.

Can anyone please guide me on this?

My configuration:

<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>

Upvotes: 2

Views: 929

Answers (1)

Michal Kordas
Michal Kordas

Reputation: 10925

FindBugs Maven Plugin behaves differently depending whether src/main/java is empty or not. If it doesn't exist (module is test-only and contains only src/test/java) build is not failing:

[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] ------------------------------------------------------------------------

When there is some production code in module (src/main/java not empty) the build fails:

[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] ------------------------------------------------------------------------

It is bug in FindBugs Maven Plugin. I've reported it as gleclaire/findbugs-maven-plugin#33. In FindbugsViolationCheckMojo there's an if checking whether 'main' classes exist and if not, plugin assumes that there was nothing analyzed and code to fail the build is not invoked.

There are two workarounds:

  • add <classFilesDirectory>${project.build.testOutputDirectory}</classFilesDirectory> to configuration
  • add any code to src/main/java

Upvotes: 2

Related Questions