How to make Maven unit test code coverage work

user3732793 picture user3732793 · Mar 24, 2016 · Viewed 31.4k times · Source

In Eclipse, I have used EcLEmma to see the unit test code coverage. Which worked fine. Therefore I have tried to use the JaCoCo plugin for Maven to see the same report with Surefire from Maven build, or even better, with a certain profile, or in the site cycle. Without success. All suggested solutions here didn't work for me.

What is the best way to get a unit test code coverage report (with surefire)?

[Edit] to be more specific why jacoco failed for me.... as I got always the Skipping JaCoCo execution due to missing execution data

from the pom in the properties

    <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
    <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>

in the Build section

        <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>jacoco-maven-plugin</artifactId>
                                    <versionRange>${jacoco.version}</versionRange>
                                    <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>${jacoco.ut.execution.data.file}</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>${jacoco.ut.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                                            </configuration>
                                        </execution>
                                        <!-- 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>${jacoco.it.execution.data.file}</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>${jacoco.it.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                                            </configuration>
                                        </execution>
                                    </executions>
                                </pluginExecutionFilter>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

was my last try but the pom becomes bigger and bigger without any result

which failes with

configuring report plugin org.apache.maven.plugins:maven-jxr-plugin:2.3

configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.5.201505241946

Skipping JaCoCo execution due to missing execution data file:......\target\jacoco.exec

Skipping JaCoCo execution due to missing execution data file:......\target\jacoco-it.exec

.... => long project path

Answer

Julien picture Julien · Apr 11, 2017

Thanks user3732793

Personnaly, I only needed to add this to my pom.xml

<project>
...

    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            ...
            <!-- Code Coverage report generation -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-code-coverage-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Then I am running

mvn test

and I get the HTML report under ./target/site/jacoco/*.html