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:

org.mockito
    mockito-inline
    5.2.0
    test

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:

org.jacoco
    jacoco-maven-plugin
    0.8.8
    
        
            prepare-agent
            
                prepare-agent
            
            
                ${project.build.directory}/jacoco.exec
                surefireArgLine
            
        
        
            report
            test
            
                report
            
            
                
                    XML

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

org.apache.maven.plugins
    maven-surefire-plugin
    
        @{surefireArgLine} -Xshare:off

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

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

✅ The Result

After applying all these settings, I ran:

mvn clean verify

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! 👇