![Jacoco logo](http://intellitech.pro/wp-content/uploads/2017/05/Jacoco.png)
Integrate jacoco with maven
Introduction
JaCoCo http://www.eclemma.org/jacoco is a free Java code coverage library distributed under the Eclipse Public License and it is very simple to add to all types of build including ANT and Maven, and it is also very simple to add to Java containers or a standalone JVM.
Configure Jacoco with Maven
Add these properties tag to pom.xml
1 2 3 4 5 6 7 |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file> <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file> <jacoco.version>0.7.5.201505241946</jacoco.version> <skip.integration.tests>true</skip.integration.tests> <skip.unit.tests>false</skip.unit.tests> |
Add this plugins to pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>pre-unit-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <destFile>${jacoco.ut.execution.data.file}</destFile> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${jacoco.ut.execution.data.file}</dataFile> <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> </configuration> </execution> <execution> <id>pre-integration-test</id> <phase>pre-integration-test</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <propertyName>failsafeArgLine</propertyName> </configuration> </execution> <execution> <id>post-integration-test</id> <phase>post-integration-test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${jacoco.it.execution.data.file}</dataFile> <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>${surefireArgLine}</argLine> <skipTests>${skip.unit.tests}</skipTests> <excludes> <exclude>**/IT*.java</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <id>integration-tests</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <argLine>${failsafeArgLine}</argLine> <skipTests>${skip.integration.tests}</skipTests> </configuration> </execution> </executions> </plugin> |
Add a test method
src/main/java/com.intellitech.jacoco.example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.intellitech.jacoco.example; public class Application { public void getGender(String gender) { if (gender.equalsIgnoreCase("Men")) { System.out.println("It's a man"); } else { System.out.println("It's a women"); } } } |
src/test/java/com.intellitech.jacoco.example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.intellitech.jacoco.example; import org.junit.Test; public class ApplicationTest { @Test public void getGenderTest() { Application application = new Application(); application.getGender("Men"); } } |
run the application
To run the examples execute the following command.
1 |
mvn clean test |
Results
The results are published in /target/site/jacoco.
We can drill through a more detailed view for each Java class:
Report Analysis
JaCoCo reports help to visually analyze code coverage by using diamonds colors for branches and background colors for lines:
Red diamond means that no branches have been exercised during the test phase.
Yellow diamond shows that the code is partially covered – some branches have not been exercised.
Green diamond means that all branches have been exercised during the test.
Source code
https://bitbucket.org/intellitech-team/jacoco