How to fail a maven build, if JUnit coverage falls below certain threshold

If you are using 0.8.2 or similar versions of jacoco-maven-plugin, please make sure data file is defined, and use the following configuration for the plugin.

<execution>
  <id>jacoco-check</id>
  <goals>
    <goal>check</goal>
  </goals>
  <configuration>
    <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
    <rules>
      <rule>
        <element>BUNDLE</element>
        <limits>
          <limit>
            <counter>INSTRUCTION</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.17</minimum>
          </limit>
        </limits>
      </rule>
    </rules>
  </configuration>
</execution>

This code ensures overall coverage of 17%.

Please verify this using

mvn clean verify

command.


JaCoCo offers that feature.

JaCoCo with Configuration rules

Define JaCoCo plugin using configuration rules COVEREDRATIO forLINE and BRANCH :

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.7.201606060606</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>check</id>
      <goals>
          <goal>check</goal>
      </goals>
      <configuration>
        <rules>
          <rule>
            <element>CLASS</element>
            <limits>
              <limit>
                <counter>LINE</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.80</minimum>
              </limit>
              <limit>
                <counter>BRANCH</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.80</minimum>
              </limit>
            </limits>
            <excludes>
              <exclude>com.xyz.ClassToExclude</exclude>
            </excludes>
          </rule>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

Various options

The supported counter options are:

  • LINE
  • BRANCH
  • INSTRUCTION
  • COMPLEXITY
  • METHOD
  • CLASS

I believe INSTRUCTION would allow you to make a general check (verify that the whole project has at least 0.80 of coverage for instance).

Example with INSTRUCTION - overall instruction coverage of 80%

This example requires an overall instruction coverage of 80% and no class must be missed:

<rules>
  <rule implementation="org.jacoco.maven.RuleConfiguration">
    <element>BUNDLE</element>
    <limits>
      <limit implementation="org.jacoco.report.check.Limit">
        <counter>INSTRUCTION</counter>
        <value>COVEREDRATIO</value>
        <minimum>0.80</minimum>
      </limit>
      <limit implementation="org.jacoco.report.check.Limit">
        <counter>CLASS</counter>
        <value>MISSEDCOUNT</value>
        <maximum>0</maximum>
      </limit>
    </limits>
  </rule>
</rules>

Message on failure

If the coverage isn't as expected, it fails with the following message:

[WARNING] Rule violated for class com.sampleapp.SpringConfiguration: lines covered ratio is 0.00, but expected minimum is 0.80
[WARNING] Rule violated for class com.sampleapp.Launcher: lines covered ratio is 0.33, but expected minimum is 0.80
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Exclusions

In the example above, I set <exclude>com.xyz.ClassToExclude</exclude>. I think you'll find you need to add many exclusions. Projects usually contain many classes which aren't testable/tested (Spring Configuration, Java beans...). You may be able to use regular expression too.


sources:

  • http://choudhury.com/blog/2014/02/25/enforcing-minimum-code-coverage
  • http://www.eclemma.org/jacoco/trunk/doc/maven.html
  • http://www.eclemma.org/jacoco/trunk/doc/check-mojo.html