2

I am upgrading my spring boot microservice from Java11 to Java17, service build and run successfully with use of maven. I have added jacoco-maven-plugin plugin for unit test cases coverage but when I tried in sonarqube I am seeing code coverage is zero. The same change is working fine with Java 11. My configuration is as below.

Maven: 3.6.1
Java: 17.0.7
maven-surefire-plugin: 2.22.2
jacoco-maven-plugin: 0.8.8
sonarqube: 9.9.2.77730

I am using below command for test case and coverage.

mvn clean test


mvn clean verify sonar:sonar \
  -Dsonar.projectKey=test-service \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=sqa_57657e1f59a8f2b720badb3b7b2043e004e3832f

Maven properties are as below:

<maven.surefire.version>2.22.2</maven.surefire.version>
<sonar.jacoco.reportPath>target/coverage-reports/jacoco-unit.exec</sonar.jacoco.reportPath>
<sonar.test.reportPath>coverage-reports/</sonar.test.reportPath>
<sonar.jacoco.coverage.output.directory>target/site/jacoco-ut</sonar.jacoco.coverage.output.directory>
<sonar.junit.reportsPath>target/surefire-reports</sonar.junit.reportsPath>
<sonar.projectName>test-service</sonar.projectName>
<sonar.skip>false</sonar.skip>

and test profile is as below:

<profiles>
    <profile>
        <id>sonar-build</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven.surefire.version}</version>
                    <configuration>
                        <!-- Jacoco prepare-agent builds some command-line params without -->
                        <!-- which jacoco will not instrument. Hence it is important to add -->
                        <!-- those command-line params here (${argLine} holds those params) -->
                        <argLine>${surefireArgLine} -Xms256m -Xmx2048m</argLine>
                        <argLine>
                            --add-opens java.base/java.time=ALL-UNNAMED
                            --add-opens java.base/java.time.format=ALL-UNNAMED
                            --add-opens java.base/java.util=ALL-UNNAMED
                            --add-opens java.base/java.util.stream=ALL-UNNAMED
                            --add-opens java.base/java.lang=ALL-UNNAMED
                        </argLine>
                        <forkCount>1</forkCount>
                        <runOrder>random</runOrder>
                        <destFile>${sonar.test.reportPath}</destFile>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.8</version>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        <append>true</append>
                    </configuration>
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <propertyName>surefireArgLine</propertyName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>jacoco-report</id>
                            <goals>
                                <goal>report</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${sonar.jacoco.coverage.output.directory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
2
  • 1
    On my system, Jacoco runs fine without any of those --add-opens options. I can’t even imagine why Jacoco should require such options. Commented Nov 13, 2023 at 8:38
  • First determine whether the problem is with Jacoco or SonarQube. You should have a directory in "target" where it is writing your Jacoco output files. If Jacoco ran, there should be an "index.html" file. Open that file in your browser. Inspect the results. If they look too low (or zero), then the problem is in your Jacoco configuration. If they are fine, then SonarQube isn't getting the Jacoco data. Commented Nov 14, 2023 at 0:13

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.