Jacoco Maven Plugin Junit Code Coverage Example

Jacoco Maven Plugin Junit Code Coverage Example

This tutorial guides you on how to use Jacoco Maven Plugin to generate code coverage for Junit Tests written in the Java Project. Jacoco is an opensource project used to check how many lines of your code is unit tested.

Jacoco Maven Plugin Junit Code Coverage Example

Let’s create a sample Java Maven Project in Eclipse and modify your pom.xml. Make sure that you have added Jacoco Maven Plugin and Junit Jupiter API dependency as shown below.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>   
    <version>0.0.1-SNAPSHOT</version>
    <groupId>com.sneppets</groupId>
    <artifactId>jacaco</artifactId>
    <name>jacaco</name>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    
    <build>
        <finalName>maven-jacaco-coverage</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.9</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
</project>

The above pom.xml Jacoco plugin configuration will generate Jacoco report during Maven test phase.

Sample Junit test for Jacaco Code Coverage

Let’s write sample application code (Message.java) under src/main/java for which we need to write junit tests.

Message.java

package com.sneppets.jacaco;

public class Message {
    
    public String getMessage(String msg) {
        
        StringBuilder sb = new StringBuilder();
        
        if(msg == null || msg.trim().length() == 0) {
            sb.append("please enter message");
        }else {
            sb.append("Thank you, " + msg);
        }
        
        return sb.toString();
    }

}

After writing application logic, now let’s write junit test for the above logic. And make sure that you create test class under src/test/java.

TestMessage.java

package com.sneppets.jacaco;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TestMessage {

      @Test
      public void testMessageRosy() {

          Message obj = new Message();
          assertEquals("Thank you, Rosy", obj.getMessage("Rosy"));

      }
      
      @Test
      public void testMessageEmpty() {

          Message obj = new Message();
          assertEquals("please enter message", obj.getMessage(""));

      }
}

Finally, its time to run mvn test and check the Jacoco code coverage report that will be generated.

Maven Unit Test Code Coverage using Jacoco

Let’s run mvn test command as shown below.

>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.sneppets:jacaco >-------------------------
[INFO] Building jacaco 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jacaco ---
[INFO] Deleting C:\Users\sneppets\Documents\Workspaces\Java\jacaco\target
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (default) @ jacaco ---
[INFO] argLine set to -javaagent:C:\\Users\\sneppets\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\
>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.sneppets:jacaco >-------------------------
[INFO] Building jacaco 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jacaco ---
[INFO] Deleting C:\Users\sneppets\Documents\Workspaces\Java\jacaco\target
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (default) @ jacaco ---
[INFO] argLine set to -javaagent:C:\\Users\\sneppets\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.2\\org.jacoco.agent-0.8.2-runtime.jar=destfile=C:\\Users\\sneppets\\Documents\\Workspaces\\Java\\jacaco\\target\\jacoco.exec
---------------
--------------
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sneppets.jacaco.TestMessage
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 s - in com.sneppets.jacaco.TestMessage
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.2:report (jacoco-report) @ jacaco ---
[INFO] Loading execution data file C:\Users\sneppets\Documents\Workspaces\Java\jacaco\target\jacoco.exec
[INFO] Analyzed bundle 'jacaco' with 1 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.263 s
[INFO] Finished at: 2022-02-05T15:30:27+05:30
[INFO] ------------------------------------------------------------------------
.8.2\\org.jacoco.agent-0.8.2-runtime.jar=destfile=C:\\Users\\sneppets\\Documents\\Workspaces\\Java\\jacaco\\target\\jacoco.exec --------------- -------------- [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.sneppets.jacaco.TestMessage [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 s - in com.sneppets.jacaco.TestMessage [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- jacoco-maven-plugin:0.8.2:report (jacoco-report) @ jacaco --- [INFO] Loading execution data file C:\Users\sneppets\Documents\Workspaces\Java\jacaco\target\jacoco.exec [INFO] Analyzed bundle 'jacaco' with 1 classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.263 s [INFO] Finished at: 2022-02-05T15:30:27+05:30 [INFO] ------------------------------------------------------------------------

Note, once you see build is success, you need to check for the Jacoco code coverage report under target/site/jacoco/index.html. Try to open that index.html file to check for the code coverage in any browser.

Jacoco Maven Plugin Junit Code Coverage Example

Also, it is evident that we have covered unit tests for all the methods and lines from the below picture.

Jacoco Maven Junit Code Coverage Example

That’s it. you had learnt how to write Junit tests and get code coverage report using Jacoco Maven Plugin.

Hope this article is helpful 🙂

You’ll also like:

References:

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments