Java + Maven + Jacoco + TeamCity integrated successfully and able to see code coverage but unit test count is not showing on Sonar dashboard

Hello Team,

Here is my pipeline configuration looks like:

TeamCity CI tool: [TeamCity Enterprise] 2020.1.5 (build 78938)
Maven: 3.5.2
Jacoco: 0.8.4
SonarQube Scanner: 4.2.0.1873
SonarQube edition: * Community Edition

Maven project: multi module
Root pom.xml configuration:

    <pluginManagement>

        <plugins>

            <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.8.0</version>

                <configuration>

                    <encoding>${project.build.sourceEncoding}</encoding>

                    <source>${project.build.javaVersion}</source>

                    <target>${project.build.javaVersion}</target>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.jacoco</groupId>

                    <artifactId>jacoco-maven-plugin</artifactId>

                    <version>${jacoco.maven.plugin.version}</version>

                    <executions>

                        <!--

                            Prepares the property pointing to the JaCoCo runtime agent which

                            is passed as VM argument when Maven the Surefire plugin is executed.

                        -->

                        <execution>

                            <id>pre-unit-test</id>

                            <goals>

                                <goal>prepare-agent</goal>

                            </goals>

                            <configuration>

                                <!-- Sets the path to the file which contains the execution data. -->

                                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>

                                <!--

                                    Sets the name of the property containing the settings

                                    for JaCoCo runtime agent.

                                -->

                                <propertyName>surefireArgLine</propertyName>

                            </configuration>

                        </execution>

                        <!--

                            Ensures that the code coverage report for unit tests is created after

                            unit tests have been run.

                        -->

                        <execution>

                            <id>post-unit-test</id>

                            <phase>test</phase>

                            <goals>

                                <goal>report</goal>

                            </goals>

                            <configuration>

                                <!-- Sets the path to the file which contains the execution data. -->

                                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>

                                <!-- Sets the output directory for the code coverage report. -->

                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>

                            </configuration>

                        </execution>

                    </executions>

            </plugin>

            <plugin>

                <groupId>org.jacoco</groupId>

                <artifactId>jacoco-maven-plugin</artifactId>

                <version>${jacoco.maven.plugin.version}</version>

                <executions>

                    <!-- The Executions required by unit tests are omitted. -->

                    <!--

                        Prepares the property pointing to the JaCoCo runtime agent which

                        is passed as VM argument when Maven the Failsafe plugin is executed.

                    -->

                    <execution>

                        <id>pre-integration-test</id>

                        <phase>pre-integration-test</phase>

                        <goals>

                            <goal>prepare-agent</goal>

                        </goals>

                        <configuration>

                            <!-- Sets the path to the file which contains the execution data. -->

                            <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>

                            <!--

                                Sets the name of the property containing the settings

                                for JaCoCo runtime agent.

                            -->

                            <propertyName>failsafeArgLine</propertyName>

                        </configuration>

                    </execution>

                    <!--

                        Ensures that the code coverage report for integration tests after

                        integration tests have been run.

                    -->

                    <execution>

                        <id>post-integration-test</id>

                        <phase>post-integration-test</phase>

                        <goals>

                            <goal>report</goal>

                        </goals>

                        <configuration>

                            <!-- Sets the path to the file which contains the execution data. -->

                            <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>

                            <!-- Sets the output directory for the code coverage report. -->

                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.22.2</version>

                <configuration>

                    <!-- Sets the VM argument line used when unit tests are run. -->

                    <argLine>${surefireArgLine}</argLine>

                    <!-- Skips unit tests if the value of skip.unit.tests property is true -->

                    <skipTests>false</skipTests>

                    <!-- Excludes integration tests when unit tests are run. -->

                    <excludes>

                        <exclude>**/IT*.java</exclude>

                    </excludes>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-failsafe-plugin</artifactId>

                <version>2.22.1</version>

                <executions>

                    <!--

                        Ensures that both integration-test and verify goals of the Failsafe Maven

                        plugin are executed.

                    -->

                    <execution>

                        <id>integration-tests</id>

                        <goals>

                            <goal>integration-test</goal>

                            <goal>verify</goal>

                        </goals>

                        <configuration>

                            <!-- Sets the VM argument line used when integration tests are run. -->

                            <argLine>${failsafeArgLine}</argLine>

                            <!--

                                Skips integration tests if the value of skip.integration.tests property

                                is true

                            -->

                            <skipTests>false</skipTests>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <artifactId>maven-archetype-plugin</artifactId>

                <version>3.0.1</version>

            </plugin>

            <plugin>

                <artifactId>maven-deploy-plugin</artifactId>

                <version>2.8.2</version>

                <executions>

                    <execution>

                        <id>default-deploy</id>

                        <phase>deploy</phase>

                        <goals>

                            <goal>deploy</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>cobertura-maven-plugin</artifactId>

                <version>2.7</version>

            </plugin>

        </plugins>

    </pluginManagement>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <configuration>

                <source>8</source>

                <target>8</target>

            </configuration>

        </plugin>

        <plugin>

TeamCity build steps:

Build step: Jacoco coverage reports are publishing to teamcity temp directory by default

Maven reports move to checkout directory step:: moving coverage reports from teamcity temp directory to %teamcity.build.checkoutDi%/JacocoReports for sonar to read reports

SonarQube step: Sonar step reading jacocoreport.xml from %teamcity.build.checkoutDi%/JacocoReports


TeamCity build Overview showing code coverage & Unit test count:

Sonar Dashboard showing code coverage but 0 unit test count

I am not sure what is missing for Sonar to publish unit test count i.e., 458 count as shown in picture

Please advise and let me know if you need any further details. Appreciate your help.

Correcting the typo in Topic description

Java + Maven + Jacoco + TeamCity integrated successfully and able to see code coverage but unit test count is not showing on Sonar dashboard

Hello @apantham,

Thanks for such a detailed report.
I see you’re using sonar.junit.reportPaths property and pass there the JaCoCo reports location. While according to the documentation this property is used for Surefire reports.

Could you please pass Surefire reports paths to this property and check whether the result is the same?

Kind regards,
Margarita

Thanks for the response @Margarita_Nedzelska.

The reason I set sonar.junit.reportPaths property and pass there the JaCoCo reports location because of two reasons:

  1. Surefire reports are generating inside each sub module where there is no way to read those reports using sonar parameters because of wildcards are not supported and our goal is to achieve it using relative paths only.

So we decided to move reports as explained below:

  1. TeamCity build step is creating surefire reports under /tempdir as shown below. Whereas second build step (powershell script) is “moving surefire reports back to checkout directory from tempdir” where Sonar (third build step) is able to read the reports that is the reason we are seeing code coverage but not total unit test count.

Thanks for the message @apantham,

It’s still not clear for me whether you have surefire reports under the provided directory (I am not sure this property supports wildcards).

And will help a lot if you could run analysis with --info and share the logs. There probably is the line “Surefire report path: {yourPath} not found.”

Another point do you analyse the whole project at once or module by module? I think you don’t need to specify all the folders but only for each module if you analyse module by module.

Regards,
Margarita