DEV Community

Cover image for How I Got SonarCloud to Detect Test Coverage in a Spring Boot Project with JaCoCo
Juan Manuel Hoyos
Juan Manuel Hoyos

Posted on

How I Got SonarCloud to Detect Test Coverage in a Spring Boot Project with JaCoCo

Recently, I ran into a pretty common problem while working on a Spring Boot project with SonarCloud: test coverage wasn't being reported correctly. Even though my tests were running fine, SonarCloud was showing 0% coverage. 🤯

After digging through the docs and trying different configurations, I finally got everything working. Here's exactly what I did, in case it helps you too.


1. Add mockito-inline dependency

One issue I had with Mockito was mocking final classes or static methods. To solve this, I added the following dependency to my pom.xml:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

2. Configure JaCoCo properly

The JaCoCo plugin needs to be configured correctly to generate the jacoco.xml file, which is what SonarCloud uses to calculate code coverage. Here’s the configuration I used in pom.xml:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <formats>
                    <format>XML</format>
                </formats>
            </configuration>
        </execution>
    </executions>
</plugin>
Enter fullscreen mode Exit fullscreen mode

You’ll also need to configure the Surefire plugin to pass the argLine property:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>@{surefireArgLine} -Xshare:off</argLine>
    </configuration>
</plugin>
Enter fullscreen mode Exit fullscreen mode

3. Configure SonarCloud

To let SonarCloud find the generated report, you need to specify the path to jacoco.xml. Add this to your configuration:

sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
Enter fullscreen mode Exit fullscreen mode

You can do this either in:

  • A sonar-project.properties file, or
  • Directly in the SonarCloud web UI:
  1. Go to your project in SonarCloud.
  2. Navigate to Administration > General Settings > JaCoCo.
  3. In the "Path to XML report" field, set:
   target/site/jacoco/jacoco.xml
Enter fullscreen mode Exit fullscreen mode

âś… The Result

After applying all these settings, I ran:

mvn clean verify
Enter fullscreen mode Exit fullscreen mode

The coverage report was generated correctly, and SonarCloud finally recognized the actual test coverage. 🎉


📝 Final Thoughts

Connecting JaCoCo and SonarCloud in a Spring Boot project isn’t always straightforward, especially if you're just getting started. Hopefully, this guide saves you some debugging time.

Did you run into a similar issue or find another solution? Let me know in the comments! 👇

Top comments (0)