maven jacoco plugin does not generate coverage report

makingitwork picture makingitwork · Mar 24, 2016 · Viewed 7.3k times · Source

I am using sonarqube as the output for the test results, while maven and jacoco for testing the test cases.

Sonarqube version is 5.4 Maven version is 3.3.9 Jacoco version 0.7

This is my pom.xml

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.sonarqube</groupId>
  <artifactId>example-ut-maven-jacoco</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Java :: UT Coverage with JaCoCo :: Maven</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- Minimal supported version is 4.7 -->
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
         <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.15</version>
        <configuration>
            <!-- Sets the VM argument line used when unit tests are run. -->
            <argLine>${surefireArgLine}</argLine>
            <!-- Skips unit tests if the value of skip.unit.tests property is true -->
            <skipTests>${skip.unit.tests}</skipTests> 
        </configuration>
    </plugin>
    </plugins>
  </build>



</project>

I am also following this link here but for the files I am using the ones here enter link description here.

This how I build the project for testing

mvn clean test
mvn clean verify
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install
mvn sonar:sonar

The buiild returns successful but there are no code coverage generated I also checked this by going to sonarqube localhost:9000/ it displays 0.00% code coverage but the unit test are all presented as successful "it is successful".

Any idea as to what might cause this problem? Also I am still new to all of this.

Answer

wemu picture wemu · Mar 24, 2016

The setup looks ok from my point of view.

You have changed the default name of the report file. Therefore I would guess you need to tell that to the sonar analyser as well.

The jacoco default for unit tests is: ${project.build.directory}/jacoco.exec Thats also the name sonar will try to pick up.

You can simply set a property to indicate the location for the sonar analyzer:

<properties
    <sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</sonar.jacoco.reportPath>
    ...

Please verify if your jacco file exists and is non empty - if so sonar will sooner or later pick it up once the location is correctly configured.