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.

  1. POM.XML- JACOCO PLUGIN

    JACOCO plugin code is added under plugins tag




    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>maths</groupId>
    <artifactId>maths</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>maths</name>
    <url>http://maven.apache.org</url>
    
    <properties>
    	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
    	<dependency>
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<version>4.4</version>
    	</dependency>
    </dependencies>
    
    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.jacoco</groupId>
    				<artifactId>jacoco-maven-plugin</artifactId>
    				<version>0.7.7.201606060606</version>
    				<executions>
    				<execution>
    					<goals>
    						<goal>prepare-agent</goal>
    					</goals>
    				</execution>
    				<execution>
    					<id>report</id>
    					<phase>prepare-package</phase>
    					<goals>
    						<goal>report</goal>
    					</goals>
    				</execution>
    			</executions>
    		</plugin>
    	</plugins>
    </build>
    
    
    </project>
    

    2.Maths.java 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. MathsTest.java Test Cases

    package maths.maths;
    
    import org.junit.Test;
    import static org.junit.Assert.*;
    
    /**
     * Unit test
     */
    public class MathsTest {
    
    	@Test
    	public void testAddition() {
    		assertEquals(20, Maths.addition(10, 10));
    	}
    
    	@Test
    	public void testSubtraction() {
    		assertEquals(0, Maths.subtraction(10, 10));
    	}
    
    	@Test
    	public void testMultiplication() {
    		assertEquals(100, Maths.multiplication(10, 10));
    	}
    
    	@Test
    	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 *