Home > Net >  Getting Sonarqube to show a code Coverage score for a simple Java/JUnit/Maven project with Jacoco pl
Getting Sonarqube to show a code Coverage score for a simple Java/JUnit/Maven project with Jacoco pl

Time:02-10

I'm practicing on a very small Java/JUnit/Maven project with the Jacoco plugin to practice with the Code Coverage feature of Sonarqube.

The core code for the example is small. Here is the class under test:

package com.somebody.sonarqube;

public class Calculator {
    
    // Method to add two numbers
    public int addNumbers(int one, int two) {
        return one   two;
    }
    
    // Method to subtract two numbers
    public int subtractNumbers(int one, int two) {
        return one - two;
    }
}

And here are the unit tests:

package com.somebody.sonarqube;

import static org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest {

    @Test
    public void testAddNumbers() {
        Calculator myCalc = new Calculator();
        assertEquals(10, myCalc.addNumbers(5, 5));
    }

    @Test
    public void testSubtractNumbers() {
        Calculator myCalc = new Calculator();
        assertEquals(5, myCalc.subtractNumbers(10, 5));
    }

}

The two methods in the class under test are covered by the two unit tests respectively, so as far as "Code Coverage" is concerned, that Sonarqube score should be 100%.

When trying to get this score after running my "clean test sonar:sonar" Maven build targets, I'm getting 0% instead:

Sonarqube score

So the part I'm having the most trouble with is getting the various components in the pom.xml file working together. At this time there's a warning at the <systemPropertyVariables> block at the bottom that reports Invalid plugin configuration: systemPropertyVariables. Here's the full 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.somebody.sonarqube</groupId>
   <artifactId>HelloSonarqube</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>HelloSonarqube</name>
   <description>Small example of Sonarqube and JUnit tests.</description>
   
   <properties>
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
   </properties>
   
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.2</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.sonarsource.scanner.maven</groupId>
         <artifactId>sonar-maven-plugin</artifactId>
         <version>3.9.1.2184</version>
      </dependency>
   </dependencies>
   
   <build>
      <plugins>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
         </plugin>
         <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.9.0</version>
            <configuration>
               <release>11</release>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <executions>
               <execution>
                  <id>prepare-agent</id>
                  <goals>
                     <goal>prepare-agent</goal>
                  </goals>
               </execution>
               <execution>
                  <id>report</id>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>report</goal>
                  </goals>
               </execution>
               <execution>
                  <id>post-unit-test</id>
                  <phase>test</phase>
                  <goals>
                     <goal>report</goal>
                  </goals>
                  <configuration>
                     <dataFile>target/jacoco.exec</dataFile>
                     <outputDirectory>target/jacoco-ut</outputDirectory>
                  </configuration>
               </execution>
            </executions>
            <configuration>
               <systemPropertyVariables>
                  <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
               </systemPropertyVariables>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

I've tried a lot of variations to this, including changing JUnit 4 to version 5 (I'd be interested to hear about issues on this too). From my reading I generally understand that the Eclipse/JUnit lifecycle is not fully working harmoniously with the Maven lifecycle by default, hence it's missing something. The solutions I've seen/tried are quite specific to certain project setups, and I have tried manually setting "arguments" and other changes. Would be really thrilled to see your opinions on this having spent a number of hours playing with different versions.

CodePudding user response:

I was able to finally answer the question based upon a similar example I just found on this GitHub project: https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-basic

In short, they used "clean verify sonar:sonar" rather than "clean test sonar:sonar" Maven build targets. The change triggered the code coverage plugin that I was looking for to publish code coverage scores in the Sonarqube dashboard.

As an aside, the warning message I also shared regarding <systemPropertyVariables> was not critical to solving the problem, and I decided to go without that part of the code, and still get all the reports in the Sonarqube dashboard I was looking for.

  • Related