In this article we are going to cover What is code coverage testing, what is Jacoco, How to Generate Jacoco report for maven Project using Jacoco maven plugin, accessing Jacoco reports in maven project.
Prerequisite:
- Preinstalled java (Java 1.5 or higher) – Follow this article to install java on Windows
- Preinstalled maven (Maven 3.0 or higher)
- You can use out Jacoreport-maven-project GitHub Repo
Table of Contents
What is Code Coverage Testing
Code Coverage Testing is a software metrics that is used to measure percentage of test cases covering of our code are executed during automated tests.
coverage reports include:
- Function coverage: How many of the functions defined have been called.
- Statement coverage: How many of the statements in the program have been executed.
- Branches coverage: How many of the branches of the control structures (if statements for instance) have been executed.
- Condition coverage: How many of the boolean sub-expressions have been tested for a true and a false value.
- Line coverage: How many of lines of source code have been tested.
What is Jacoco
JaCoCo stands for Java Code Coverage. It is free and open source code coverage library for Java.
We can integrate Jacoco in various tools like Gradle, Maven, Eclipse, Eclipse and many more. JaCoCo.
We can integrates Jacoco with CI tools like Jenkins, GitHub Actions , GitLab, Circle CI, etc. with the help of the JaCoCo maven plugin.
Step #1:Create Maven Project
Go to Run and type ‘cmd‘ to open Command Prompt
Browse to the folder where you want to set up your project and then type the below command:
mvn archetype:generate -DgroupId=devopsHint -DartifactId=DevopsHint -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
It will create build and will create maven project OR you can use out Jacoreport-maven-project GitHub Repo
Step #2:Adding jacoco-maven plugin in pom.xml
Open pom.xml file in project’s root folder, add below code as jacoco-maven plugin
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/reports</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Step #3:Adding String palindrome java program in maven project
We are replacing default java program created using maven project with below string palindrome program in below locations
main/src/main/java/com/devopshint/StringPalindrome/App.java
package com.devopshint.StringPalindrome;
public class App {
public boolean isPalindrome(String input) {
if (input == null) {
throw new IllegalArgumentException("input shouldn't be null");
}
if (input.equals(reverse(input))) {
return true;
} else {
return false;
}
}
private String reverse(String input) {
String rev = "";
for (int i = input.length() - 1; i >= 0; i--) {
rev = rev + input.charAt(i);
}
return rev;
}
}
Step #4:Adding String palindrome java program in maven project junit test
We are replacing default java program created using maven project junit test with below string palindrome program in below locations
main/src/test/java/com/devopshint/StringPalindrome/AppTest.java
package com.devopshint.StringPalindrome;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest {
String input1 = "noon";
App app = new App();
boolean expected = true;
@Test
public void isPlaindromeTest() {
assertEquals(expected, app.isPalindrome(input1));
}
@Test
public void isNotPlaindromeTest() {
assertEquals(false, app.isPalindrome("abc"));
}
@Test(expected = IllegalArgumentException.class)
public void isNotPlaindromeExceptionTest() {
assertEquals(false, app.isPalindrome(null));
}
}
Step #5:Generate Jacoco Report for Maven Project
Build and test maven project to Generate Jacoco Report for Maven Project, Using mvn clean test command we buid the project execution of tests, Jacoco agent will automatically instrument to the code, thus, it will create a coverage report in binary format in the target directory – target/jacoco.exec
mvn clean test
Output:
Step #6:Accessing Jacoco code coverage report in maven project
To Access coverage report navigate to the target > reports > jacoco > index.html > right-click > Open In > Browser > And your preferred browser. So basically index.html is your code coverage report file. So you can see your report will be shown like this and the percentage completely depends on how you have written the test cases.
Jacoco Code coverage colors:
Green: Code is tested fully or covered.
Yellow: Code is partially tested or covered
Red: Code is not tested or covered
Conclusion:
We have covered What is code coverage testing, what is Jacoco, How to Generate Jacoco report for maven Project using Jacoco maven plugin, accessing Jacoco reports in maven project.
Related Articles:
Reference: