Jacoco is the software metric used to provide how many lines of code covered in each source java file after executing the unit tests.
In this article, we are going to learn how to automate the Jacoco tool to invoke using the Maven build tool to provide the coverage report.
JaCoCo Maven Configuration:
Jacoco is providing the Maven plugin to get started and create the source code coverage report.
Add the below maven plugin configuration in the pom.xml file.
Running the tests using Junit, will automatically detect the Jacoco agent, it will create the Coverage report in the data file specified in the plugin configuration.(target/jaoco.exec). This file will be in a binary file, and it is not human readable. But we can upload the exec file like some tools like SonarQube etc. Which can read the exec file, and provide the report in a human-readable format?
Jacoco Maven plugin has the goal report to generate the human-readable format like HTML file, below is the maven command to generate the report.
This maven goal will create index.html file outputDirectory specified in Jacoco plugin configuration if not target/site/jacoco/index.html page will be created by the plugin.
Jacoco also provides the Eclipse plugin EclEmma plugin, using which we can create the code coverage tool using defaults.
As Jacoco used byte code instructions to generate the report, it will check for all cyclomatic complexity and so on.
Jacoco report will have 3 different colors for different purposes.
1) Red diamond:
It means that the lines code/no branches of the conditions not covered in unit tests.
2) Yellow diamond:
It means some branches of the code are covered, and some branches of the code not covered during the tests.
3) Green diamond:
It means all lines of the code/all branches of the code covered during the tests.
Jacoco provides mainly three important metrics:
1) Line Coverage:
It reflects how many lines of code are executed based on java byte code instructions called from test cases.
2) Branch Coverage:
It shows how many branches of the statement in code, called from test cases, like if/else and switch statements.
3) Cyclomatic complexity:
Reflects the complexity of the code by providing the number of paths required to cover all possible paths to the code by linear combination.
Jenkins: A Continuous Integration Tool
As based on Maven, it can also run in continuous integration tools like Jenkins. After a developer commits the code, we can execute the test with code coverage, and we can put some threshold value for minimum coverage percentage so that without writing unit test Jenkins will not allow committing the code, which can improve the code quality the project.
Execution inside Jacoco plugin
Add the below execution inside the Jacoco plugin configuration:
If the Code coverage is less than 90%, then build fail, in turn, the Jenkins job failed.
Jacoco also provides another goal to simply check code coverage percentage without generating the report.
Get a clear picture of the facility provided by the JaCoCo source code coverage tool by visiting us and get a complete report.
mvn jacoco:check is the maven command to verify the coverage percentage.
Configuring the plugin using Maven surefire plugin:
Maven surefire plugin used to execute the tests, Jacoco provides the JVM agent is responsible for invoking the Jacoco Analysis tool.
During the execution time, surefireArgLine property will be resolved and add the Jacoco JVM agent, and it will trigger the Code coverage analysis tool.
A Leading Java Application Development Company here shows you what is Jacoco and How to use this tool by integrating the Maven build tool, different color Conventions used in the Jacoco report. We learned the different Maven goals used to trigger the Jacoco maven plugin, and finally, we learned how to integrate with the maven surefire plugin to trigger the analysis.