DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Unit Test Code Coverage

Unit Test Code Coverage

John Dobie user avatar by
John Dobie
·
Jan. 18, 12 · Interview
Like (0)
Save
Tweet
Share
9.50K Views

Join the DZone community and get the full member experience.

Join For Free
It’s easy to collect unit test code coverage because all of the common tools are geared up for it.
This article will explain how you can add unit test coverage to your Maven application in 10 minutes.
We will use the excellent Jacoco code coverage library to show how easy it is.

Examples

All of the examples come from this article.
http://johndobie.blogspot.com/2011/11/test-doubles-with-mockito.html

You can check them out from here
svn co
https://designbycontract.googlecode.com/svn/trunk/examples/testing/test-doubles


With Maven 3 installed, you can run them with this command.
mvn clean package

What is Jacoco

Jacoco is a free code coverage library for Java. http://www.eclemma.org/jacoco/
I use it because it is very simple to add to all types of build including ANT and Maven, and it is also very simple to add to Java containers or a standalone JVM.


How Does it Work?


Jacoco uses the standard JVM Tool Interface. http://java.sun.com/developer/technicalArticles/J2SE/jvm_ti/
In simple terms you attach a Jacoco agent to a JVM when it starts. It was introduced in JDK 5 for monitoring and profiling JVMs and being able to dynamically modify Java classes as they're being loaded.
Whenever a class is loaded Jacoco can instrument the class so it can see when the class is called and what lines are called. That’s how it builds up the coverage statistics. This is all done on the fly.
By default the results file is created when the JVM terminates. You can also run the agent in server mode which allows you to trigger a dump of the results.

Attaching the agent to the JVM?

This is a very simple process. You must specify where the jacoco jar is located and then you pass some parameters to define how the agent is to run.
-javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]
A typical run might look like this
-javaagent:jacoco.jar=destfile=${sonar.jacoco.itReportPath},includes=com.dbc.*
A full reference is found here. http://www.eclemma.org/jacoco/trunk/doc/agent.html

Jacoco Support For Maven

The docs for the maven plugin are defined here. http://www.eclemma.org/jacoco/trunk/doc/maven.html
First we need to add the plugin itself.
<plugin>
  <groupid>org.jacoco</groupid>
  <artifactid>jacoco-maven-plugin</artifactid>
  <version>0.5.5.201112152213</version>
</plugin>
We can then define where the jacoco reports are output.
  ${basedir}/target/coverage-reports/jacoco-unit.exec
  ${basedir}/target/coverage-reports/jacoco-unit.exec

Finally we need to define the following 2 executions to make the agent run before the tests are run and also to make sure that the jacoco report task is run when package is executed.
<executions>
  <execution>
    <id>jacoco-initialize</id>
    <goals>
      <goal>prepare-agent</goal>
    </goals>
  </execution>
  <execution>
    <id>jacoco-site</id>
    <phase>package</phase>
    <goals>
      <goal>report</goal>
    </goals>
  </execution>
</executions>
The full pom.xml can be found here pom

To run the examples execute the following command.
mvn clean package 

Results.


The results are published in /target/site/jacoco.
The original article can be found here http://johndobie.blogspot.com/2012/01/unit-test-code-coverage.html
unit test Code coverage Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Rust vs Go: Which Is Better?
  • Master Spring Boot 3 With GraalVM Native Image
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: