14

I'm using JUnit5 on a SpringBoot backend application server using Maven. Here is the sonar-project.properties file that is at the root of the project:

sonar.host.url=https://sonarcloud.io
sonar.login=xxx
sonar.organization=xxx
sonar.projectKey=xxx

sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.java.source=12

sonar.sources=src/main/java
sonar.test=src/test
sonar.java.binaries=target/classes
sonar.junit.reportPaths=target/test-results/TEST-**.xml

I use the sonar-scanner command line to run update the project after a build/test.

The Overview board on sonar-cloud looks like this:

overview

I at least got the unit tests to be recognized, but somehow I'm still at 0% in terms of code coverage. Furthermore, here is the Measures board:

measures

Apparently, my tests do not cover any lines whatsoever. Now, I'm aware that this means that I most probably didn't hook up the test-results properly, but I'm not sure how to do that.

What puzzles me, too, is that despite SonarQube recognizing my tests, it actually says that the lines-of-code of the tests themselves aren't tested. What is this supposed to mean?

Testing tests?

6
  • Did you run the tests after setting up Sonarqube? The code coverage is populated with the data from a run. You also need to set you the coverage variables. docs.sonarqube.org/latest/analysis/coverage Commented Nov 23, 2019 at 19:32
  • This project has had Sonarqube set up for 1 week now. But I just started adding unit tests, hence why I did not realize that this part of it wasn't working properly. Which variable is the "coverage variable" ?
    – payne
    Commented Nov 23, 2019 at 19:35
  • "SonarSource analyzers do not run your tests or generate reports. They only import pre-generated reports. Below you'll find language- and tool-specific analysis parameters for importing coverage and execution reports." You need to use jacoco or similar tools. Commented Nov 23, 2019 at 19:36
  • Yes, I knew about that. Using mvn test generates a surefire report: that is what I'm trying to plug in there, right? It's just hard to know which ensemble of variables should be determined.
    – payne
    Commented Nov 23, 2019 at 19:38
  • 1
    I think it is not enough. You need to use a code coverage tool such as jacoco. Commented Nov 23, 2019 at 19:39

3 Answers 3

21

From SonarQube's documentation:

SonarSource analyzers do not run your tests or generate reports. They only import pre-generated reports.

A popular library for generating code coverage for Java is Jacoco.

SonarQube provides this guide to create and import Jacoco's reports.

2
  • 1
    I somehow overlooked the fact that "execution" and "coverage" are two separate things. Your answer made me realize my error. Thank you! I will mark it appropriately. Eventually, I will edit it with a working piece of sonar-project.properties file.
    – payne
    Commented Nov 23, 2019 at 19:47
  • SonarQube is a beautiful tool, but they are many bits needed to make everything work. Commented Nov 23, 2019 at 19:48
6

Here is the working sonar-project.properties file:

# SONAR CLOUD CONFIGS
sonar.host.url=https://sonarcloud.io
sonar.organization=xxx
sonar.projectKey=xxx
sonar.login=xxx

# SOURCES
sonar.java.source=12
sonar.sources=src/main/java
sonar.java.binaries=target/classes
sonar.sourceEncoding=UTF-8

# EXCLUSIONS
# (exclusion of Lombok-generated stuff comes from the `lombok.config` file)
sonar.coverage.exclusions = **/*Exception.java , **/MySpringBootApplication.java

# TESTS
sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
sonar.junit.reportsPath=target/surefire-reports/TEST-*.xml
sonar.tests=src/test/java
3

We have same issue. You need to add coverage blocks in your pom.xml Can refer following sample pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.test testapp 0.0.1-SNAPSHOT testapp Demo project for Spring Boot

<properties>
    <java.version>1.8</java.version>
    <sonar.coverage.jacoco.xmlReportPaths>../target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
      <groupId>org.sonarsource.scanner.maven</groupId>
      <artifactId>sonar-maven-plugin</artifactId>
      <version>3.6.0.1398</version>
    </plugin>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
    </plugin>
    </plugins>
</build>

<profiles>
<profile>
  <id>coverage</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <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>

Reference URL: https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-basic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.