Code coverage is a metric indicating which percentage of lines of code are executed when running automated tests. Unit and integration tests for instance.
It’s known that having automated tests as part of your build process improves the software quality and reduces the number of bugs.
Do you know if you need more unit tests? Or if your tests cover all possible branches of an if or switch statements? Or if your code coverage is decreasing over time? Especially after you join a team to work on an on-going project.
Code coverage helps to answer these questions. This post covers reporting code coverage using Maven’s jacoco-maven-plugin, a library that adds minimal overhead with normal build.
- Java 7+.
- Maven 3.2+.
- Overview of my previous post, Splitting Unit and Integration Tests using Maven and Surefire plugin because this post uses the same source code.
3. SAMPLE APPLICATION
The example application has two unit test classes,
DefaultSomeBusinessServiceTest, and two integration tests classes,
ApplicationTests; similar to those discussed in Splitting Unit and Integration Tests using Maven and Surefire plugin section.
4. CONFIGURING jacoco-maven-plugin AND COVERAGE THRESHOLD
0or set forkMode to
neveras it would prevent executing the tests with the JaCoCo
javaagentand no coverage would be recorded.
Let’s configure jacoco-maven-plugin in
prepare-agent goal sets up the property
argLine (for most packaging types) pointing to the JaCoCo runtime agent. You can also pass
argLine as a VM argument. maven-surefire-plugin uses
argLine to set the JVM options to run the tests.
If you are explicitly setting
argLine, make sure it allows late replacements like:
so that maven-surefire-plugin picks up changes made by other Maven plugins such as jacoco-maven-plugin.
The JaCoCo Java agent will collect coverage information when maven-surefire-plugin runs the tests. It’ll write it to
destFile property value if set, or
target/jacoco.exec by default. Read more at https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html.
report goal creates code coverage reports for tests in HTML, XML, CSV formats. Stay tuned, I’ll cover uploading code coverage reports to SonarQube in another post. This goal reads the
dataFile property value if set, or
target/jacoco.exec. And writes the resulting reports to
outputDirectory property value or
target/site/jacoco. Read more at https://www.eclemma.org/jacoco/trunk/doc/report-mojo.html.
check goal validates the coverage rules (discussed later) are met. In case they are not, it interrupts and fails the build unless
haltOnFailure property is set to
false. Read more at https://www.eclemma.org/jacoco/trunk/doc/check-mojo.html.
5. RUNNING THE TESTS AND CREATING THE COVERAGE REPORTS
Let’s build the application and analyze the Maven command output:
Right after the
clean phase completes, jacoco-maven-plugin’s
prepare-agent goal (bound to the Maven’s Build Default Lifecycle’s
initialize phase) sets the
argLine property pointing to the JaCoCo Java agent.
Unit and Integration tests ran separately as covered in a previous post.
Next, not included in this log output, the Maven artifact is built and repackaged.
After that, jacoco-maven-plugin’s
coverage-report goal (bound to the Maven’s Build Default Lifecycle’s
post-integration-test phase) generates HTML, XML and CSV reports.
Opening the HTML report at
target/site/jacoco/index.html results in:
Code coverage report for a successful build
check goal (bound to the Maven’s Build Default Lifecycle’s
verify phase) checks the code coverage metrics are met.
6. JACOCO RULES
Let’s take a closer look at the jacoco-maven-plugin’s
coverage-check rules configuration in pom.xml:
Setting the rule element to
CLASS means every Java class from the application would need to meet each counter limit for the build to pass.
In this example there is only one limit, a
LINE counter that needs a coverage of at least 80%. I’ll cover JaCoCo Counters later.
Other JaCoCo rules you could used are:
|BUNDLE||The set of counter limits would have to be met at the application as a whole|
|PACKAGE||The set of counter limits would have to be met for all packages (eg
|CLASS||The set of counter limits would have to be met for every Java class|
|METHOD||The set of counter limits would have to be met for every class method|
Notice that has you move down in the JaCoCo rules table, the
check goal becomes more constraining.
As an example, if you remove
com.asimio.demo.Application from the
excludes sections, the build fails because the
LINE counter doesn’t reach 80% for said class:
7. JACOCO COUNTERS
|INSTRUCTION||The amount of code that can be executed or missed|
|BRANCH||The total number of branches (if and switch statements) in a method that can be executed or missed.
|LINE||Executed when at least one instruction that is assigned to this line has been executed.
|METHOD||Executed when at least one instruction has been executed|
|CLASS||Executed when at least one of its methods has been executed|
Notice that has you move down in the JaCoCo counters table, the
check goal becomes less constraining.
Examples of associating a counter to a rule are:
value is one of:
and a numeric
Although not a silver bullet, code coverage helps to measure what percentage of code is executed when running the test suites. And thus, helping to reduce the number of bugs and improving the software release quality.
Keeping a certain threshold might get difficult over time as a development team adds edge cases or implement defensive programming.
JaCoCo adds minimal overhead to the build process. jacoco-maven-plugin’s
prepare-agent goal, bound to the
initialize phase, sets the agent responsible for instrumenting the Java code before maven-surefire-plugin runs.
coverage-report goal is bound to the
post-integration-test phase. And
coverage-report goal is bound to the
verify phase. Read more at Maven’s Build Default Lifecycle. This means, unlike other libraries, JaCoCo doesn’t need to run the tests twice.
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
9. SOURCE CODE
Accompanying source code for this blog post can be found at:
ABOUT THE AUTHOR
Orlando L OteroOrlando L Otero is a Software Engineer Consultant at FedEx Express, focusing on integration, microservices, API design and implementation and agile delivery.
This website includes affiliate links to Google. This means if you click an affiliate link and buy a product, I might earn a commission at no extra cost to you.