Test Coverage with JaCoCo2

Introduction

Jacoco (Java code Coverage Tools) is an Open-source code coverage library. It is used to measure how many lines of code are being executed during the Unit Test or Automated Test. It was created by the EclEmma team and is distributed under the Eclipse public License.

There are various open-source coverage technologies available for measuring Java code coverage, but JaCoCo stands out due to its performance, comprehensive coverage metrics, support for modern Java features, ease of integration, and active development. These factors make JaCoCo a preferred choice for many development teams looking to maintain high code quality and thorough test coverage. 

Measuring the test coverage of your application is essential to understand quality, maintainability, and overall software reliability. Some key reasons why a JaCoCo report is important are:  

  1. Ensuring Comprehensive Test Coverage.
  2. Identifying untested code. 
  3. Improving code quality. 
  4. Facilitating continuous Integration. 
  5. Encouraging better testing practices.    

This blog aims to provide valuable insight and practical understanding on how to use the JACOCO Code coverage tool to anyone who is working as a Java Developer to improve code coverage and overall code quality.   

Concept Breakdown

JaCoCo runs as a Java agent and instruments the byte code while running the tests. It validates each instruction and shows which line of code is executed during each test.

Test Coverage with JaCoCo

To generate the coverage report we attach the JaCoCo agent to JVM when it starts. Whenever JVM loads the Java class, JaCoCo instruments the class and identifies when the class is called or what line of code is called during the test phase. By keeping track of this record JaCoCo generates a coverage report in the second stage. As soon as the JVM terminates it creates the file.  

JaCoCO agent call also be run in server mode, in this case, tests are run with JaCoCo: Dump to initiate the dump request.  

Pre-requisite to Configure JaCoCO

  • Download a library from here ( Select the most recent version). 
  • Extract the Downloaded .zip file and go to the folder \jacoco-0.8.12\lib 

The jacocoagent.jar should be copied into the project library.  

Possible Ways to Configure JaCoCo

There are several ways to configure the JaCoCo agent to generate code coverage reports. Some of these methods include:

Ant Configuration:

  • Follow the steps mentioned in the pre-requisite and copy Jacocoant.jar and jacocoagent.jar in your project lib folder.  
  • Open the ant build script of your project and define properties pointing to the folder path where the Jacoco.exec and Jacoco coverage report files will be created.  
  • In the ant script look for the target that runs the Junit test cases (if there is no such target to run unit test cases create one) and add JVM arguments. 
  • Add taskdef Url and build report target  
Test Coverage with JaCoCo

The build report reads the execution data file (Jacoco.exec) which is created while running the Junit test cases and creates the JaCoCo report from the execution data file.  

The above build report will generate html file for report coverage by using the jacoco.exec (Execution Data) File one can also generate CSV and XML type files. 

Maven Configuration:

To configure JaCoCo agent and generate a coverage report we need to declare the maven plugin in pom.xml . 

Test Coverage with JaCoCo

Report Generation 

Running Junit test cases will automatically set in motion the JaCoCo agent and will create a coverage report in binary format Jacoco.exec then we use jacoco:report goal to generate human readable code coverage reports in formats, like HTML, CSV, and XML. 

Now we have a look at the human-readable report target/site/jacoco/index.html page to see what the generated report looks like: 

Test Coverage with JaCoCo

We can drill more to see the detailed view for each class by clicking on the individual package name.

Test Coverage with JaCoCo
  • Red Diamond indicates that no branch has been covered during the test phase.  
  • Green Diamond indicates that the branch is covered during the test phase. 
  • Yellow Diamond indicates that the branch is partially covered during the test phase.  

Report Metrics

JaCoCo mainly provides 3 important metrics:

  • Lines Coverage reflects the amount of code covered during the test coverage-based Java Byte Code instruction called by the test execution.
  • Branches Coverage reflects the branches covered during the test, typically related to if/else and switch statements. 
  • Cyclomatic Complexity reflects the code complexity by determining the independent path executions.   

Conclusion

The JaCoCo Report provides valuable insight into the test coverage of the JAVA project, Offering a clear understanding of how well the Java application is tested and can enhance the robustness and reliability of your application.