Code Coverage is showing 0% for java projects

Hi,

We have recently upgraded our sonar application from 7.1 to 7.9.6 (LTS in 7.x).
We have observed that code coverage is showing 0% for java projects using maven as build tool and jacoco as code coverage.
Please note that we are getting non zero code coverage for sonar 7.1 (Before upgrade) for same java projects.

We have observed that jacoco plugin property sonar.jacoco.reportPaths which was being used in Sonar 7.1 is no longer supported in Sonar 7.9.6.
Instead, we are using updated property sonar.coverage.jacoco.xmlReportPaths in pom.xml file.

We are using below code snippet for code coverage in our pom.xml file:

<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>test.junitmaven.project</groupId>
    <artifactId>JunitMavenProject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    
<properties>
        <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
        <sonar.language>java</sonar.language>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.sources>src/main</sonar.sources>
        <sonar.tests>src/test</sonar.tests>
        <sonar.java.binaries>target/classes</sonar.java.binaries>
        <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
   </properties>

   <dependencies>
       <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.1</version>
         <scope>test</scope>
       </dependency>
     </dependencies>

     <build>
           <plugins>
             <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.1</version>
               <configuration>
                         <source>1.8</source>
                         <target>1.8</target>
               </configuration>
             </plugin>
              <plugin>
               <groupId>org.sonarsource.scanner.maven</groupId>
               <artifactId>sonar-maven-plugin</artifactId>
               <version>3.7.0.1746</version>
             </plugin>

             </plugins>
     </build>

     <profile>
       <id>coverage</id>
       <build>
        <plugins>
         <plugin>
               <groupId>org.sonarsource.scanner.maven</groupId>
               <artifactId>sonar-maven-plugin</artifactId>
               <version>3.7.0.1746</version>
             </plugin>
         <plugin>
         <groupId>org.jacoco</groupId>
         <artifactId>jacoco-maven-plugin</artifactId>
         <version>0.8.6</version>
         <executions>
           <execution>
             <id>prepare-agent</id>
             <goals>
              <goal>prepare-agent</goal>
             </goals>
           </execution>
           <execution>
             <id>report</id>
             <goals>
              <goal>report</goal>
             </goals>
           </execution>
         </executions>
         </plugin>
        </plugins>
       </build>
     </profile>
</profiles>
</project>

Please note that we have removed some lines from pom.xml which contains organization specific information.

Jacoco version installed: 1.1.0 (build 898)
Java Code Quality and Security: 6.3.2 (build 22818)

One observation: We are able to see non zero code coverage in target/site/jacoco/index.html file
whereas we still see 0% code coverage on sonar server UI. Attaching the screenshot for the reference.

I would request to help us to understand what we are missing.

Thanks,
Vaibhav Jariwala

Hi,

Congratulations on your upgrade to 7.9.6. And you need to keep going. You should upgrade to either the latest version or the current LTS at your earliest convenience. Your upgrade path is:

xxx → 7.9.6 → 8.9.2 → 9.1 (last step optional)

You may find the Upgrade Guide and the LTS-to-LTS Upgrade Notes helpful. If you have questions about upgrading, feel free to open a new thread for that here.

Regarding your actual question, it’s not worth trying to debug this in an EOL version. Once you’ve consulted the LTS-to-LTS Upgrade Notes, you’ll see that the accepted report format for JaCoCo has changed & you’ll need to catch that up. And if there’s still a problem at that point, then please come back to us.

 
Ann

Hi @ganncamp ,

Thanks for replying back.

We are actually upgrading from 7.1 to 8.9.2. We had to upgrade to 7.9.6 because that’s supported path for upgrade.
In order to stabilise our environment @7.9.6, we need to fix this issue.
Hence we need a help.

Thanks,
Vaibhav Jariwala

Hi Vaibhav,

Yes, I understand. But this is just a waypoint. There’s no point in spending time to stabilize this when you know you’re just going to move right past it. Go ahead & do the upgrade to 8.9.1 and then we can work on this.

 
Ann

Hi @ganncamp ,
I have the same problem and I have SonarQube version 8.9.0.43852. Please, is it possible that the coverage is not working due to the wrong version?

Some debug info

Sensor JaCoCo XML Report Importer [jacoco]
Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.

Sonar scanner info

Project scanner properties:
  - sonar.coverage.jacoco.xmlReportPaths=/correctPath/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml
  - sonar.junit.reportPaths=/correctPath/analytics-ui/test/build/test-results/singlemethod_Java00
Scanner properties of module: CoverageJaCoCoUnit:analytics-ui
  - sonar.coverage.jacoco.xmlReportPaths=/correctPath/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml
....

We have several dozen modules and the coverage of the .xml file is created by merging the jacoco.exec file from the individual modules.

Our plugin info
SonarQube plugins:

  • Python Code Quality and Security 3.4.1.8066 (python)
  • CSS Code Quality and Security 1.4.2.2002 (cssfamily)
  • Go Code Quality and Security 1.8.3.2219 (go)
  • JaCoCo 1.1.1.1157 (jacoco)
  • Kotlin Code Quality and Security 1.8.3.2219 (kotlin)
  • JavaScript/TypeScript Code Quality and Security 7.4.2.15501 (javascript)
  • Ruby Code Quality and Security 1.8.3.2219 (ruby)
  • Scala Code Quality and Security 1.8.3.2219 (sonarscala)
  • C# Code Quality and Security 8.22.0.31243 (csharp)
  • Groovy 1.8 (groovy)
  • Java Code Quality and Security 6.15.1.26025 (java)
  • HTML Code Quality and Security 3.4.0.2754 (web)
  • Flex Code Quality and Security 2.6.1.2564 (flex)
  • XML Code Quality and Security 2.2.0.2973 (xml)
  • PHP Code Quality and Security 3.17.0.7439 (php)
  • VB.NET Code Quality and Security 8.22.0.31243 (vbnet)

Please can you advise me what’s going on? Would an update help?

Hi @ganncamp and @puci ,

I am pleased to inform you that our issue is resolved.
I would like to share the fix so that anyone facing the same issue may get the help.

Possible Causes:

  • Unit test cases are missing.
  • sonar-maven-plugin is not defined in pom.xml file.
  • Language and tool-specific maven plugin (for an example, Jacoco) is not defined in pom.xml
  • Jacoco plugin related configuration in pom.xml is not correct.

Solutions:

  • SonarQube doesn’t run your tests or generate reports. It only imports pre-generated reports. We have to use language and tool-specific analysis parameters for importing coverage and execution reports. Please ensure that you have at least one unit test case defined for your project.

  • Please ensure that sonar-maven-plugin is defined in pom.xml:

<plugin>
  <groupId>org.sonarsource.scanner.maven</groupId>
  <artifactId>sonar-maven-plugin</artifactId>
  <version>3.7.0.1746</version>
</plugin>
  • Please ensure that jacoco related configuration is correct:
<plugin>
   <groupId>org.jacoco</groupId>
   <artifactId>jacoco-maven-plugin</artifactId>
   <version>0.8.6</version>
   <executions>
      <execution>
          <id>prepare-agent</id>
          <goals>
             <goal>prepare-agent</goal>
          </goals>
          <phase>test-compile</phase>
      </execution>
      <execution>
          <id>report</id>
          <goals>
             <goal>report</goal>
          </goals>
          <phase>test</phase>
      </execution>
    </executions>
</plugin>
  • If your jacoco reports are generated at custom path (other than default path: target/site/jacoco/jacoco.xml ) OR if you are using deprecated sonar property( sonar.jacoco.reportPaths ) for jacoco then please update sonar property as below:
    <sonar.coverage.jacoco.xmlReportPaths>your/custom/path/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

Reference: Test Coverage & Execution | SonarQube Docs

3 Likes

@jariwala I’m afraid the issue will definitely not solve me (as I wrote in the post, I have the right paths and current properties) and I use gradle with correct sonar plugin, but thank you for trying help me …

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.