2

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.

makingitwork
  • 149
  • 2
  • 9
  • Why are you running `mvn clean test` and `mvn clean verify` ...and `mvn clean ... install` ? Running a single `mvn clean install` and may be `mvn sonar:sonar` would be sufficient. – khmarbaise Mar 24 '16 at 11:38
  • @khmarbaise I read that you should test and verify first? or what are just the necessary steps to accomplish what i want? I just tried everything i read. – makingitwork Mar 24 '16 at 11:51
  • Hi, What java version are you using? – Francisco Hernandez Mar 24 '16 at 11:51
  • @FranciscoHernandez jdk1.8.0_65 and jre8 – makingitwork Mar 24 '16 at 11:52
  • With that version you have to have java 8 compatibility, try to remove the configuration part for the report goal, maybe that is confusing sonarqube. Look at this http://stackoverflow.com/questions/32993959/mavens-code-coverage-for-java8-project – Francisco Hernandez Mar 24 '16 at 11:59
  • @FranciscoHernandez can you help me create a full pom.xml based on that. I am really new to this and don't know much about pom.xml – makingitwork Mar 24 '16 at 12:01
  • If the problem continues despite of applying the workarounds, you may have a look at my answer on [maven jacoco: not generating code coverage report](https://stackoverflow.com/questions/25395255/maven-jacoco-not-generating-code-coverage-report/71661614#71661614). – Murat Yıldız Mar 29 '22 at 12:03

1 Answers1

2

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.

wemu
  • 7,952
  • 4
  • 30
  • 59