Maven sample does not work with JUnit 5 - Zero coverage

Starting with the sample at https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule

Change the dependency from JUnit 4 to JUnit 5

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.6.0</version>
  <scope>test</scope>
</dependency>

Then, change the import for the Test annotation in every test class to

import org.junit.jupiter.api.Test;

Running the example before the change shows the correct coverage report. Running the build again “mvn clean verify sonar:sonar” will build successfully but show a coverage report of zero.

For this sample test, I used the image published on Docker Hub.

docker pull sonarqube:latest

I tried moving the sample to the latest versions of jacoco-maven-plugin and org.sonarsource.scanner.maven but the result was the same.

<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.5</version>
</plugin>

I am having the same problem in all my projects (different version of sonarqube), but I thought it easier to discuss by eliminating the other variables and using this sample with the latest versions.

–Derek

Hello @Derek

SonarQube is not computing any coverage by itself, it only imports reports from other tools (in this case JaCoCo).

If the coverage is 0%, I can see two explanations:

  • The report is not (correctly) generated.
    I would make sure that I can see coverage report of JaCoCo, without even running any SonarQube analysis.
    I’m sure you will find plenty of information online in order to make this work correctly.

  • The report is here, but not imported.
    In this case, I advise you to have a look at this guide. In addition, logs may contain useful information.

If the report is correctly generated, imported as suggested by the guide, and logs do not contain any clues, feel free to come back to us.

Best,
Quentin

Thanks @Quentin. I was reporting that the sample produced from Sonar Source would not work with JUnit 5. I traded messages with the Jacoco team, and found the issue. The sample from Sonar Source does not specify the Surefire version in the POM. Unfortunately, the default version of Surefire will not work, and needs to be updated. Please let the person or team that manages those samples know that it would be best if they included (so that the sample works with both Junit4 or Junit5):

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.2</version>
    </plugin>
1 Like

Thanks for letting us know how you resolve this problem, it will hopefully help others!