How to generate code coverage report in java? Jacoco | Graphical Report

Code coverage is a software metric used to measure how many lines of our code are executed during automated tests. Jacoco is code coverage automatic graphical report generator for Java project.

I hope you all know what is maven? And by the time you know maven, you also know pom.xml. To be exact jacoco is a plugin we need to add in the pom.xml and you get report automatically. Show it to management to impress if it is good 🙂

Here illustrating example of mathematical functions with test cases and code coverage graphical report.


    JACOCO plugin code is added under plugins tag

    <project xmlns="" xmlns:xsi=""
 Main class

    package maths.maths;
     * Maths!
    public class Maths {
    	public static void main(String[] args) {
    		System.out.println("Hello World!");
    	public static int addition(int num1, int num2) {
    		return (num1+num2);
    	public static int subtraction(int num1, int num2) {
    		return (num1-num2);
    	public static int multiplication(int num1, int num2) {
    		return (num1*num2);
    	public static int division(int num1, int num2) {
    		return (num1/num2);

    3. Test Cases

    package maths.maths;
    import org.junit.Test;
    import static org.junit.Assert.*;
     * Unit test
    public class MathsTest {
    	public void testAddition() {
    		assertEquals(20, Maths.addition(10, 10));
    	public void testSubtraction() {
    		assertEquals(0, Maths.subtraction(10, 10));
    	public void testMultiplication() {
    		assertEquals(100, Maths.multiplication(10, 10));
    	public void testDivision() {
    		assertEquals(1, Maths.division(10, 10));

  4 .Run – mvn clean package 

Go to project directory and write  mvn clean package.
Your report is all ready in three formats namely HTML, CSV and XML.
Go to  Project Name —>  Target —> Site —-> Jacoco

Browse index.html and you can see something like this:-
1-Total lines and missed lines
2-Total functions and missed functions

If you further elaborate, you can see line wise coverage:-

Hope you can generate graphical report now.Still any query? Post a comment,
Also Find what is Bitcoin?
What is Java9?

3 thoughts on “How to generate code coverage report in java? Jacoco | Graphical Report

  1. I haven¦t checked in here for a while because I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *