JaCoCo is a free Java code coverage tool. This is essentially the successor to Emma, and it has been developed by the EclEmma team as an Eclipse project.
JaCoCo offers line and branch coverage.
Maven is set up on your system and is verified to work. To do this, refer to the first three recipes of Chapter1, Getting Started.
Use the following steps to analyze the code coverage with the Maven JaCoCo plugin:
project-with-tests
).<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.2.201409121644</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
mvn package
[INFO] --- jacoco-maven-plugin:0.7.2.201409121644:prepare-agent (default-prepare-agent) @ project-with-tests --- [INFO] argLine set to -javaagent:C:\software\maven\org\jacoco\org.jacoco.agent\0.7.2.201409121644\org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=C:\projects\apache-maven-cookbook\project-with-tests\target\jacoco.exec [INFO] --- jacoco-maven-plugin:0.7.2.201409121644:report (default-report) @ project-with-tests --- [INFO] Analyzed bundle 'Project with Tests with 1 classes
index.html
file generated in the target/site/jacoco
folder:In the pom file, we instruct Maven to run the following two goals of the Maven JaCoCo plugin:
prepare-agent
: This is bound by default to the initialize
phase of the Maven default lifecycle. The goal runs and prepares the agent that does the analysis.report
: This agent gathers test coverage information when the tests are run and creates the report as part of the prepare-package phase (which we have explicitly specified).The report gives information about the test coverage. Green indicates lines that are covered by tests and red indicates lines that are not covered by tests. In the preceding example, 12 of 19 instructions are not covered by tests.
You could subject the project to code coverage and generate the same report without making any changes to the pom file. To do this, run the following command:
mvn jacoco:prepare-agent test jacoco:report
Now, you may get the following error:
[ERROR] No plugin found for prefix 'jacoco' in the current project and in the plugin groups [org.apache.maven.plugins] available from the repositories [local (C:softwaremaven), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
To fix this, specify the groupId
and artifactId
parameters of the plugin explicitly. In the Configuring Maven to search for plugins recipe of Chapter 8, Handling Typical Build Requirements, we will see an alternate way to address this.
In the following code, what what we will be doing is explicitly calling the relevant goals that we saw getting executed earlier. So, first prepare-agent
will run, followed by test
, and then the report
goal:
mvn org.jacoco:jacoco-maven-plugin:prepare-agent test org.jacoco:jacoco-maven-plugin:report
How about failing the build if the code coverage is below a threshold value? To do this, perform the following steps:
build
section specified earlier:<execution> <id>default-check</id> <phase>prepare-package</phase> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.60</minimum> </limit> </limits> </rule> </rules> </configuration> </execution>
mvn package
3.15.229.111