Java Test - code coverage using Jacoco

05 February 2017


Java code coverage

In Java there are a wide variety of code coverage tools out there. One of the most popular is jacoco which provides an easy setup if you're using maven or gradle. This post will be showing how to setup a java project in maven using jacoco code coverage.


1. Maven dependency

Insert maven dependency for jacoco in your maven project:

<dependencies>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.8</version>
    </dependency>
</dependencies>

2. Maven plugin

Insert maven plugin for jacoco in your maven project:

<build>
  <plugins>
     <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${org.apache.maven.plugins.maven.maven-compiler-plugin.version}</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
      </plugin> 
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.8</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <!-- implementation is needed only for Maven 2 --> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <!-- define your test coverage treshold % for accepting the build --> <limit implementation="org.jacoco.report.check.Limit"> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.99</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build>

3. Sample Unit test

package sample.test.coverage;

public class User { private String name; public User(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isValid() { return (name != null && !name.equals("") && name.length() <= 10); }
}

Unit test:

package sample.test.coverage;

import org.junit.Assert; import org.junit.Test;

public class UserTest { / * Tests set and get */ @Test public void testUserGettersAndSetters() { User user = new User("joe"); user.setName(""); Assert.assertTrue(user.getName().equals("")); user.setName(null); Assert.assertTrue(user.getName() == null); user.setName("java"); Assert.assertTrue(user.getName().equals("java")); } @Test public void testUserIsValid() { User user = new User("joe smith"); Assert.assertTrue(user.isValid()); } / * Tests user name null / @Test public void testUserNameNull() { User user = new User(null); Assert.assertTrue(!user.isValid()); } /** * Tests user name empty / @Test public void testUserNameEmpty() { User user = new User(""); Assert.assertTrue(!user.isValid()); } /* * Tests user name too Long / @Test public void testUserInavlidNameTooLong() { User user = new User("joe the world wide clock field service man"); Assert.assertTrue(!user.isValid()); } }


4. Run maven build and see test coverage report

run mvn clean install or mvn test to trigger the unit test execution. Once tests are finished check file {PROJECT_FOLDER}/target/site/jacoco/index.html for coverage report (open in any browser):

TEXT screenshot


5. Links

Code coverage


comments powered by Disqus