Jacoco: Production Ready Code Coverage.

Jacoco code coverage library

JVM-based frameworks have several open source coverage technologies. Considering java’s penetration and usage currently in the software industry (particularly in open source environments) the list of coverage tools shouldn’t hold any surprises. But when it comes to the actual implementation of Java-based projects (with standard stack including Spring, Struts, Velocity, Tapestry, Maven, Grunt) these open source coverage technologies start to fall a little short of expectations.

Most of them are specifically fit to a particular use case such as Ant tools or IDE plugin (Mainly eclipse) and do not offer a good documentation nor clean API, both of which would allow developers to embed in different context or architecture designs. The biggest problem is that none of them are actively maintained these days, and, therefore, don’t support current java versions.

So after some research, I encountered Jacoco code coverage library. Jacoco seems to have taken a different path altogether to provide a new standard technology for code coverage analysis in JVM based environment. It provides a very lightweight, flexible and well-supported online community, works with all major development frameworks and standard build tools, and due to its open source nature it’s found a place in many tool vendors, IDE plugins, and open-source projects. Considering these factors, I decided to use Jacoco in Java, Spring based application with Maven build tool support.

Important Features

  • Coverage analysis of an application with Package, Classes, methods and line level with most important cyclomatic complexity.
  • Maven plugin to collect coverage information and create reports integrating with Maven builds.
  • Good performance with minimal runtime overhead especially for large-scale projects.
  • Lightweight implementation with minimal dependencies on external libraries and system resources.
  • Support report formats (HTML, XML, CSV).
  • Regression tests with full functional test coverage based on Junit test cases.
  • Framework agnostic: Smoothly integrates with Java VM based applications like plain Java programs.
  • Compatible with all released JDK.
  • Simple integration through Java agent based on-the-fly the instrumentation. Based on Java bytecode and, therefore, works also without source files.

Quick Setup.

Maven (pom.xml)

Report Generation.

Jacoco reports get generated in each full maven build lifecycle.

Eg. ‘mvn clean install.’

Report generation can be tied up with different maven lifecycle phases like pre/post unit or integration tests as well. From above configuration, were report generation has been tied to the package maven phase then following simple command would also work. Eg. mvn package Maven build can also be tied to test Coverage percentage. Build will fail in case coverage ratio drops beyond specified level.

Report Location.

Reports are generated at the following standard location.

<Project_Location>/webapp/target/site/jacoco/index.html

Report location can be moved to the different location using ‘outputDirectory’ property in maven configuration.

<configuration>
    <outputDirectory>${project.reporting.outputDirectory}/jacoco-reports</outputDirectory>
</configuration>

Sample Reports.

Reports are generated with Green and Red color indicators with the Green portion representing the code covered in tests while Red indicates the portion not covered. It can also be viewed with different scope level (Class, Method… etc). The following snapshot covers most of the scopes with detailed statistics.

Scope: Package Level


Scope: Class Level


Scope: Method Level


Scope: Line Level



References