I Test First!

TDD Functional BigData ravings

Sonar Unit and Integration Test Coverage with Maven

on April 25, 2013

Lots of posts on the web about this, few seem to work. Here is my config, which works.

First, configuration of the maven sonar plugin:

<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>  <!-- Ensure you run mvn install before sonar:sonar -->
<sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin>
<sonar.surefire.reportsPath>/target/surefire-reports</sonar.surefire.reportsPath>
<sonar.jacoco.reportPath>target/jacoco.exec</sonar.jacoco.reportPath>    <!-- This is the default, put here to be explicit -->
<sonar.jacoco.itReportPath>target/jacoco-it.exec</sonar.jacoco.itReportPath>

Next, the Jacoco plugin. Here we use the default output file for unit tests, and a separate output file for integration tests:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.2.201302030002</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>target/jacoco-it.exec</destFile>
                <propertyName>failsafe.argLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>target/jacoco-it.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>

Finally, the failsafe plugin. This ensures tests will be instrumented during the integration test phase, and the results collected during the verify phase. The reference to argLine is critical, because this causes failsafe to write to the correct Jacoco output file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.14</version>
    <configuration>
        <argLine>${failsafe.argLine}</argLine>
    </configuration>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
About these ads

9 responses to “Sonar Unit and Integration Test Coverage with Maven

  1. kevin.m.coy@gmail.com says:

    I followed this post and was able to see the default jacoco.exec get produced; however, the jacoco-it.exec is not being generated. Instead, I am getting

    [INFO] — jacoco-maven-plugin:0.6.0.201210061924:report (post-integration-test) @ web-ui —
    [INFO] Skipping JaCoCo execution due to missing execution data file

  2. adrianosu says:

    Check failsafe is actually running tests as expected, e.g.

    [INFO] — maven-failsafe-plugin:2.14:verify (default) @ com.adrian —
    [INFO] Failsafe report directory: C:\trunk\adrian\services\target\failsafe-reports

    i.e. check failsafe-reports folder has some content.

    And check Jacoco is running in the pre-integration-test phase:

    [INFO] — jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (pre-integration-test) @ com.adrian —
    [INFO] failsafe.argLine set to -javaagent:C:\.m2\repository\org\jacoco\org.jacoco.agent.6.2.201302030002\org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=build\adrian\services\target\jacoco-it.exec

  3. Thank you! So many months and this never worked (Lots of different examples/documentation didn’t work at all!) You’re a godsend!

    Weird issue, possible bug: It works in general but for some reason the “Unit Tests run successfully” has dropped to 0. Is it because it’s grouping them differently now?

    • adrianosu says:

      Thanks! Your unit tests should still be run by SureFire so it shouldn’t report zero if there are unit tests. Integration tests are picked out by Failsafe with the format *IT.java so these not appear as unit tests. I would follow the Maven output carefully to see what it is doing.

      • apolischuk says:

        Hmm, got same issue:
        Unit test success
        0 tests
        Unit tests works fine and correct report was generated before adding this configuration. Also I see that jococo was run after unit test and after integrations tests, but looks like integration tests were counted as unit tests and sonar shows on dashboard 0% of integration tests.
        Unit test coverage is show correctly except the issue I described above.

      • apolischuk says:

        Oops, my mistake, put config into surefire plugin instead failsafe.
        Now it works fine. Thank you for your post. Nice job.

  4. András says:

    This article was really helpful

    BUT

    all is running fine but jacoco says (jacoco.exec is being generated):
    “Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?”

    AND

    (i am using sonar to display the info, sonar plugin shows a widget with 0% coverage)

    FINALLY

    i got it: the problem was the folder of jacoco.exec. jacoco.exec should be generated in folder of parent module (reactor). i.e. i set it to “${project.basedir}/../target/jacoco-it.exec”:

    ${project.basedir}/../target/jacoco-it.exec

    and for sonar:
    ${project.basedir}/../target/jacoco-it.exec
    now the sonar widget shows coverage >0. the warning still occurs for all submodules

    Its not spam, just I hope it will help others..

    source: http://sourceforge.net/p/eclemma/discussion/614870/thread/00d851e8

    I’m not the author but fighted with right the same issue.

    Anyway, your article is awsome! Big big thanks! :)

    • András says:

      Some characters are removed (escape-unescape, whatever) so just have a look at the ‘source’ if you are interested or the admin maybe can correct my previous comment and remove this. :)

  5. Champaka R says:

    Thanks.. I was able to follow through with the above settings and able to get jacoco coverage file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: