Missing Unit Test Coverage but have Unit Test Statistics

12,314

Solution 1

I was missing a property, called sonar.binaries. So I added the line

project-java.sonar.binaries=target/classes

to the properties file and that did the trick.

Solution 2

Can you try doing mvn clean install?

Share:
12,314
Jacob Schoen
Author by

Jacob Schoen

Born and raised in Southeast Louisiana, graduated with CS degree from the University of New Orleans and currently working towards Masters degree in Computer Science also. I have always been interested in computers, and becoming a programmer seemed a natural progression for me. It fits my personality and gives me a new puzzle to solve everyday.

Updated on June 04, 2022

Comments

  • Jacob Schoen
    Jacob Schoen almost 2 years

    I have a Jenkins Sonar setup, with a maven project. This maven project has a single pom, but in contains java, javascript, html, etc. So for sonar it is a multi module project so that I can get the statistics for each part.

    We also want to to get the code coverage analysis for the project, which means we need to run the tests and export them before the sonar analysis (at least from what I have gathered sonar-runner can not do this type of anaysis).

    So I have my jenkins job set up to do clean package as the first step, and then we run we add the Invoke Standalone Sonar Analysis as the second step. We have a sonar-project.properties file in the root of the project with all the various settings. Here it is:

    sonar.projectKey=Project
    sonar.projectName=Project
    sonar.projectVersion=0.2.0-SNAPSHOT
    sonar.projectDescription=Super Project
    sonar.modules=project-java,project-web,project-js
    project-java.sonar.dynamicAnalysis=reuseReports
    project-java.sonar.core.codeCoveragePlugin=jacoco
    project-java.sonar.dynamicAnalysis=reuseReports
    project-java.sonar.junit.reportsPath=target/surefire-reports
    project-java.sonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec
    project-java.sonar.projectName=project-java
    project-java.sonar.language=java
    project-java.sonar.projectBaseDir=.
    project-java.sonar.sources=src/main/java
    project-java.sonar.java.source=1.7
    project-web.sonar.projectName=project-web
    project-web.sonar.language=web
    project-web.sonar.projectBaseDir=.
    project-web.sonar.sources=src/main/webapp/partials
    project-js.sonar.projectName=project-js
    project-js.sonar.language=js
    project-js.sonar.projectBaseDir=.
    project-js.sonar.sources=src/main/webapp/js
    

    Now in my pom I added the following (followed along from Creating Code Coverage Reports for Unit And Integration Tests with The JaCoCo Maven Plugin for the most part):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.16</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>${skip.unit.tests}</skipTests>-->
            <!-- Excludes integration tests when unit tests are run. -->
            <excludes>
                <exclude>**/IT*.java</exclude>
            </excludes>
        </configuration>
    </plugin>   
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.6.4.201312101107</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.build.directory}/jacoco-ut</outputDirectory>
                 </configuration>
             </execution>
        </executions>
    </plugin>
    

    So when I do mvn clean package locally everything runs, and I can go to the target directory, I see the reports that were generated (I know sonar does not use these, but it has the code coverage information in it). Now when the Sonar job runs everything seems to work fine, but the results are missing the Unit Test Coverage numbers:

    Sample Output

    Looking through the build logs I found:

    14:39:43.929 INFO  - Sensor JaCoCoSensor...
    14:39:43.932 INFO  - Project coverage is set to 0% since there is no directories with classes.
    14:39:43.932 INFO  - Sensor JaCoCoSensor done: 3 ms
    

    I have seen “Project coverage is set to 0%” – JaCoCo and Sonar in Jenkins, and tried the suggestions there to no avail. So I assume I am missing another setting or have something set wrong.

  • Jacob Schoen
    Jacob Schoen about 10 years
    We must have posted at about the same time. Anyway, turns out I was missing a property that was needed to tell jacoco where the compiled class files where located. Somehow missed that, or it was assumed knowledge in all the examples I went through before. Thanks for the suggestion though.