Search code examples
javamavencode-coveragecoberturamaven-cobertura-plugin

Cobertura check is not failing the build


I created a basic maven package. The src/main/java directory contains:

public class Blah {
    public int blah(){
        return 1;
    }

    public int bluh(){
        return 2;
    }
}

The src/test/java directory contains:

import static org.junit.Assert.assertEquals;


import org.junit.Test;


public class BlahTest {
    @Test
    public void blahTest() {
        Blah b = new Blah();
        assertEquals(1, b.blah());
    }
}

And the pom is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cob_test</groupId>
    <artifactId>cob_test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <check>
                        <haltOnFailure>true</haltOnFailure>
                        <branchRate>75</branchRate>
                        <lineRate>85</lineRate>
                        <totalBranchRate>75</totalBranchRate>
                        <totalLineRate>85</totalLineRate>
                        <packageLineRate>75</packageLineRate>
                        <packageBranchRate>85</packageBranchRate>
                    </check>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>

The various parameters in the check portion are not validated when I run mvn install. As there are 2 functions, I am expecting coverage to be 50% and the expected coverage minimums are higher than that. So, the build should fail. Also, is there a way of showing the package level coverage numbers right after the build on the command line instead of having them in the html files.

The detailed split is helpful but I also want to fail a build when the minimum coverage restrictions are violated.


Solution

  • You have to execute

    mvn verify
    

    to run the cobertura, as it is default run in the verfiy lifecycle phase.

    If you want to change that you can modify your plugin configuration as follows (change the phase from verify to your liking):

    <project>
      <build>
         <plugins>
           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <check>
                        <haltOnFailure>true</haltOnFailure>
                        <branchRate>75</branchRate>
                        <lineRate>85</lineRate>
                        <totalBranchRate>75</totalBranchRate>
                        <totalLineRate>85</totalLineRate>
                        <packageLineRate>75</packageLineRate>
                        <packageBranchRate>85</packageBranchRate>
                    </check>
                </configuration>
                <executions>
                  <execution>
                    <phase>verify</phase>
                      <goals>
                        <goal>check</goal>
                      </goals>
                  </execution>
                </executions>
            </plugin>
         </plugins>
      </build>
    </project>