JaCoCo Java Code Coverage + Maven example
Jacoco is used to measure the code coverage of application. In this tutorial we will understand how to configure Jacoco in maven and how to use Jacoco to see code coverage report.
Technologies Used:
- Eclipse Mars
- Maven 3.3.9
- Java 8
1. Eclipse Create Maven Java Project
1.1 In Eclipse create a Maven project File->New->Project->Maven Project
, Select create a simple project and click on next
1.2 Enter groupId & artifactId as shown in below screen and click on finish.
2. Project Structure
The project has the following components
- Sample Arithmetic Operations Class
- Sample Arithmetic Operations JUnit Test Class
pom.xml
with Junit and Jacoco dependencies
3. pom.xml
Configure Junit and Jacoco as shown in pom.xml
<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>com.mkyong</groupId>
<artifactId>MathOperations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mathematical Operations</name>
<properties>
<jacoco.version>0.7.5.201505241946</jacoco.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<skipMain>true</skipMain>
<skip>true</skip>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</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/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
4. Sample Arithmetic Operations Class
In this class method ‘add’ is created which accepts two integer parameters and returns sum.
package math.operation;
public class ArithmeticOperations {
public Integer add(Integer a, Integer b)
{
return a+b;
}
}
5. Sample Arithmetic Operations Junit Test Class
Testcase is created for ‘add’ method.
package math.operation;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ArithmeticOperationsTest {
@Test
public void testAdd()
{
ArithmeticOperations operations = new ArithmeticOperations();
Integer actual = operations.add(2, 6);
Integer expected = 8;
assertEquals(expected, actual);
}
}
6. Run Application
Right Click Project->Run as ->Maven test. Jacoco output report will be generated in target directory under jacoco-ut folder
7. Output
7.1 To see the output go to target directory and open index.html from jacoco-ut folder in browser. Overall Report for class ArithmeticOperations
is shown below
7.2 Clicking on each method in above figure gives detailed report. Here it shows green coloured line indicating which line is covered by unit test.
Done.
Thank you for the post . I just imported your project as it is but getting msg as – “Skipping JaCoCo execution due to missing execution data file:” and no jacoco file getting generated . COuld you please help.
Right click and Build Project. Then Run As > Maven test
Getting the same error. I got output once though. But after couple of runs. It’s giving me this error indefinitely
You will need to remove the and tags.
After some modifications this was my result: https://github.com/philard/Syntax-highlighter/blob/master/pom.xml
thanks so much for this post…appreciate you !
In the execution section of the jacoco plugin, i see the report goal is attached to the test phase and prepare-package phase. What’s the point of attaching the report goal to the prepare-package phase. The prepare-package phase comes after the test phase, that means report would be generated in the test phase and would be already available for the prepare-package phase. So, why we need to generate the report again in the prepare-package phase ??
Hi, Trying to generate a jococo report for a test used for Restful services. I have used Rest assured JUnit tests for testing rest services but jococo is saing 0% coverage :(.
Maven Artifact Id should be in lowercase, according to official Maven naming conventions.
Hi Yong. Thanks for your post. if possible to please post how to get Integration test coverage report by executing the test manually. For example deploying the war to Tomcat and then how to integrate Jacoco agent + source code to get coverage report. Thanks.
Good tutorial