How to get Cobertura to fail M2 build for low code coverage

I'm trying to configure my WAR project build to fail if the line or branch coverage is below given thresholds. I've been using the configuration provided on page 455 of the excellent book Java Power Tools, but with no success. Here's the relevant snippet of my project's Maven 2 POM:

<build>
...
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <check>
        <!-- Per-class thresholds -->
        <lineRate>80</lineRate>
        <branchRate>80</branchRate>
        <!-- Project-wide thresholds -->
        <totalLineRate>90</totalLineRate>
        <totalBranchRate>90</totalBranchRate>
      </check>
      <executions>
        <execution>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
        <execution>
          <id>coverage-tests</id>
          <!-- The "verify" phase occurs just before "install" -->
          <phase>verify</phase>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <instrumentation>
        <excludes>
      <exclude>au/**/*Constants.*</exclude>
        </excludes>
        <ignores>
      <ignore>au/**/*Constants.*</ignore>
        </ignores>
      </instrumentation>
    </configuration>
  </plugin>
  ...
</plugins>
...
</build>

As I say, the coverage report works fine, the problem is that the "install" goal isn't failing as it should if the line or branch coverage is below my specified thresholds. Does anyone have this working, and if so, what does your POM look like and which version of Cobertura and Maven are you using? I'm using Maven 2.0.9 and Cobertura 2.2.

I've tried Googling and reading the Cobertura docs, but no luck (the latter are sparse to say the least).


Asked by: Audrey477 | Posted: 28-01-2022






Answer 1

To my knowledge, if the <haltOnFailure> element is set to true and any of the specified checks fails, then Cobertura will cause the build to fail which is what you're asking for. But actually, this element defaults to true if you do not specify it so you don't have to add it to your configuration checks. Failing the build below any coverage threshold is (or at least should be) the default behavior.

EDIT: I did some further testing and haltOnFailure seems to be working as expected on my environment (Maven 2.2.1. and versions 2.3, 2.2, 2.1 of the plugin i.e. versions 1.9.2, 1.9, 1.8 of cobertura on Linux). I'm updating this answer with the result below.

Actually, I've added an <execution> element to my pom. I may be misinterpreting the part of cobertura:check's documentation that says it "Binds by default to the lifecycle phase: verify" but, without the <execution> element, cobertura:check wasn't triggered during the verify phase of my build. Below the setup I've use for the cobertura-maven-plugin:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <check>
            <!--<haltOnFailure>true</haltOnFailure>--><!-- optional -->
            <!-- Per-class thresholds -->
            <lineRate>80</lineRate>
            <branchRate>80</branchRate>
            <!-- Project-wide thresholds -->
            <totalLineRate>90</totalLineRate>
            <totalBranchRate>90</totalBranchRate>
          </check>
        </configuration>
        <executions>
          <execution>
            <phase>verify</phase>
            <goals>
              <!--<goal>clean</goal>--><!-- works if uncommented -->
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

I get the following result when running mvn clean install on a freshly generated maven project (with mvn archetype:create) patched with the plugin configuration mentioned above:

$ mvn archetype:create -DgroupId=com.mycompany.samples -DartifactId=cobertura-haltonfailure-testcase
...
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building cobertura-haltonfailure-testcase
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory /home/pascal/Projects/cobertura-haltonfailure-testcase/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0-SNAPSHOT.jar
[INFO] Preparing cobertura:check
[WARNING] Removing: check from forked lifecycle, to prevent recursive invocation.
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/cobertura
Cobertura: Saved information on 1 classes.
Instrument time: 337ms

[INFO] Instrumentation was successful.
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [cobertura:check {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 1 classes.

[ERROR] com.mycompany.samples.App failed check. Line coverage rate of 0.0% is below 80.0%
Project failed check. Total line coverage rate of 0.0% is below 90.0%

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Coverage check failed. See messages above.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18 seconds
[INFO] Finished at: Sat Oct 24 21:00:39 CEST 2009
[INFO] Final Memory: 17M/70M
[INFO] ------------------------------------------------------------------------
$ 

I didn't test with maven 2.0.9, but on my machine, haltOnFailure generates a BUILD ERROR and halt the build. I don't see any differences with your plugin configuration, I can't reproduce the behavior you describe.

Answered by: Emily919 | Posted: 01-03-2022



Answer 2

Add the following to the <check/> configuration.

<haltOnFailure>true</haltOnFailure>

Answered by: Richard239 | Posted: 01-03-2022



Answer 3

mvn clean install -Dcobertura.skip=true

Answered by: Kirsten716 | Posted: 01-03-2022



Similar questions

java - Exclude methods from code coverage with Cobertura

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not drive down the coverage numbers. I know that Clover has such a functionality, but I have not found anything similar for Cobertura.


java - Zero code coverage with cobertura 1.9.2 but tests are working

I run the code coverage target: &lt;junit fork="yes" dir="${basedir}" failureProperty="test.failed"&gt; &lt;!-- Note the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important. --&gt; &lt;classpath path="${instrumented.dir}" /&gt; &lt;classpath path="${classes.dir}" /&g...


java - Where can I find a single summary coverage number in Cobertura?

I run Cobertura on my codebase and get coverage numbers by the class, line, and branch. Besides this, I am looking for a single summary number which tells me the coverage for my whole codebase. That's not enough of course; the detailed reports are essential. But having a single number gives us a metric which we can continually improve. I don't see such a number in the reports. Where can I find it?


java - How do I get Emma or Cobertura, with Maven, to report coverage on source code in other modules?

I have a multi-module Maven setup with Java code. My unit tests, in one of the modules, exercise code in multiple modules. Naturally, the modules have inter-dependencies, and code in all relevant modules is compiled as needed in advance of test execution. So: How can I get a report on the coverage of the entire codebase? Note: I am not asking how to combine the results of coverage for tes...


java - Cobertura coverage and the assert keyword

My line coverage for unit tests measured by Cobertura is suffering, because I have assert statements which are not covered in tests. Should I be testing assertions, and is there any way to get Cobertura to ignore them so they do not affect my test coverage?


java - Cobertura code coverage results not complete

I am using Cobertura for code coverage analysis. If I run a build in Jenkins the classes in generated are contained in the coverage result but the coverage is at 0%. If I run code coverage in my workspace (Eclipse) the coverage is much higher. The coverage for the package com.my.package is ok. Have I missed some configuration? My projects structure is as following: - co...


java - Cobertura code coverage is 0% when using Maven 3

After reading this: What is the proper way to use Cobertura with Maven 3.0.2 and this: http://www.wakaleo.com/blog/292-site-generation-in-maven-3 my POM file looks like this: &lt;build...


java - How to get code coverage for new code in Cobertura?

I have a JAVA project which use Cobertura for code coverage report. When I implement new code, how could I get the code coverage data for the new code only?


java - How to run cobertura code coverage for few test classes not all of my test classes?

I am using Cobertura in eclipse to generate the code coverage for my java maven project and it works fine. I have around 15 tests classes. Whenever I am running maven build, it generates code coverage for all those 15 classes. Is there any way, I can run cobertura code coverage report for few of my test classess instead of all the test classes? I also have AllTests in which I have mentioned only few of...


java - Cobertura 2.0.3 and Sonar not showing code coverage

I am trying to get Cobertura code coverage to work for a multi-package project. I had to update from 2.5.2 to 2.6 to work correctly with JDK 1.7 and now I am able to run and create cobertura code coverage reports. The files for the project are stored on a repo and are being built using Jenkins. I have been able to get Cobertura to run correctly through Jenkins as well, and the reports can be seen through Jenkins; ...


java - Exclude methods from code coverage with Cobertura

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not drive down the coverage numbers. I know that Clover has such a functionality, but I have not found anything similar for Cobertura.


java - Cobertura ant script is missing Log4J classes

I tried to get Cobertura running inside my ant script, but I'm stuck right at the beginning. When I try to insert the cobertura taskdef I'm missing the Log4J libraries. Ant properties &amp; classpath &lt;property name="cobertura.dir" location="/full/path/to/cobertura-1.9.3" /&gt; &lt;path id="cobertura.classpath"&gt; &lt;fileset dir="${cobertura.dir}"&gt; &lt...


java - Zero code coverage with cobertura 1.9.2 but tests are working

I run the code coverage target: &lt;junit fork="yes" dir="${basedir}" failureProperty="test.failed"&gt; &lt;!-- Note the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important. --&gt; &lt;classpath path="${instrumented.dir}" /&gt; &lt;classpath path="${classes.dir}" /&g...


java - Exclude specific methods from code coverage from cobertura?

I was trying to ignore all the toString() methods from instrumentation using following configuration. This wasn't really working? This is using cobertura as maven plugin. This was based on a previous answer Exclude methods from code coverage with Cobertura. &lt;instrumentation&gt; &lt;ignores&gt; ...


java - Where can I find a single summary coverage number in Cobertura?

I run Cobertura on my codebase and get coverage numbers by the class, line, and branch. Besides this, I am looking for a single summary number which tells me the coverage for my whole codebase. That's not enough of course; the detailed reports are essential. But having a single number gives us a metric which we can continually improve. I don't see such a number in the reports. Where can I find it?


java - How do I get Emma or Cobertura, with Maven, to report coverage on source code in other modules?

I have a multi-module Maven setup with Java code. My unit tests, in one of the modules, exercise code in multiple modules. Naturally, the modules have inter-dependencies, and code in all relevant modules is compiled as needed in advance of test execution. So: How can I get a report on the coverage of the entire codebase? Note: I am not asking how to combine the results of coverage for tes...


java - cobertura vs clover

Closed. This question is opinion-based. It is not c...


java - Cobertura coverage and the assert keyword

My line coverage for unit tests measured by Cobertura is suffering, because I have assert statements which are not covered in tests. Should I be testing assertions, and is there any way to get Cobertura to ignore them so they do not affect my test coverage?


java - cobertura in eclipse

I just installed ecobertura plugin, but it looks like not a very user-friendly tool from within eclipse. is there any good plugin for cobertura in eclipse or article to describe how to use ecobertura? I'm used to seeing code complexity and coverge etc in a very nice way. I need to use cobertura, because the build system in the back-end uses it, so it makes sens to use it in the IDE as well. Regards


java - Is it worth to use Cobertura in web project?

I have never used Cobertura or similar tools before, only JUnit (and I've checked what are mocking tools) and in recent days Selenium. I am writing web application with full of JSP, JavaScript (jQuery) and HTML. Can Cubertura helps me in these areas or this tools covered only plain Java classes?






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top