top of page

Jacoco code coverage

  • Writer: Anand Nerurkar
    Anand Nerurkar
  • Aug 13, 2024
  • 2 min read

Let us impement simple example and see how we can use Jacoco code coverage.


This is my class for which I am going to check code coverage


public class Messages {


public String getMessage(String name)

{

StringBuilder s = new StringBuilder();

if(name == null || name.trim().length() == 0)

{

s = s.append("Please Provide a name!");

}

else

{

s.append("Hello " + name + "!");

}

return s.toString();

}

}


In above class , we have a method getMessage, need to cover test scenario for it as a part of code coverage.


Please see below test cases for the above in test packag


public class TestMessages {


@Test

public void testName()

{

Messages obj = new Messages();

Assertions.assertEquals("Hello Anand", obj.getMessage("Anand"));

}


}


Please include code coverage plugin in pom.xml as below


<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<version>0.8.5</version>

<executions>

<execution>

<goals>

<goal>prepare-agent</goal>

</goals>

</execution>

<!-- attached to Maven test phase -->

<execution>

<id>report</id>

<phase>test</phase>

<goals>

<goal>report</goal>

</goals>

</execution>


</executions>

</plugin>

</plugins>

</build>


do the maven build and run it with goal clean test


This jacoco code coberage plugin will generate the report based on the test cases provided for the getMessage() and generatea report with index.html file where we can see code coverage


ree

click in index,html

ree

click on each class and see code coverage , missing line for code coverage


ree

if we look at above code report, yellow and red line -no code coverage , so we need to provide test coverage

there are 2 condition for which 2 test cases we need to provide

  1. name is empty

  2. name is null


Please add below test cases


@Test

public void testNameBlank()

{

Messages obj = new Messages();

Assertions.assertEquals("Please Provide a name!", obj.getMessage(""));

}


@Test

public void testNameNull()

{

Messages obj = new Messages();

Assertions.assertEquals("Please Provide a name!", obj.getMessage(null));

}



pls do maven build- clean test, check report as below

ree

ree
ree

now we can see in report , for getMessage(), every line is covered for code coverage as it indicate green color.


now we need to implement this as a part of CICD pipeline , need to configure code coverage as say 90%, we need to add other execution phase for jacoco code plugin as below


<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>


execute maven clean verify goals to check for this coverage in report.


if it above that or =, then it will build the artifacts else wont generate jar file.



 
 
 

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page