Tuesday, December 5, 2017

Code Coverage - JaCoCo Tutorial


JaCoCo is a very popular tool for code coverage now. Comparing with Cobertura, it has the benefit that it is totally based on Java Byte code so it can work without source files. Its remote protocol and JMX control supports requesting the execution data dumps from the coverage agent at any point in time, which makes life much easier for analyzing the code coverage for integration test. Most import it is able to compute the code coverage for unit test together with integration test, which gives you a better report on how the code coverage like for the whole project.

As what it says at the official site

JaCoCo should provide the standard technology for code coverage analysis in Java VM based environments. The focus is providing a lightweight, flexible and well documented library for integration with various build and development tools.


Coverage Analysis Mechanism
Let's first see how JaCoCo works. Here is a diagram coming from the JaCoCo Documentation. It gives an overview on the techniques for code coverage and the highlighted ones are the method JaCoco used.



Coverage Implementation Techniques

JaCoCo instrument the byte code by hooking a Java Agent to the JVM. It use the ASM for instrumentation.

The Jave Agent is made available in Java 1.5 for monitoring and profiling JVMs. It is able to dynamically modify Java classes as they are being loaded. The JVM starts with the agent, and Whenever the classes is loaded, the JaCoCo will instrument the classes. It collects the data when the class , method or lines are called on fly and generate the report files when the JVM terminates.

JaCoCo with Maven Project
Here I am using a sample project to demonstrate how JaCoCo works with Maven projects.
My Maven project has the class Palindrome.java and PalinedromeTest.java as below

public class Palindrome {

    public boolean isPalindrome(String inputString) {
    if (inputString.length() == 0) {
    return true;
    } else {
    char firstChar = inputString.charAt(0);
    char lastChar = inputString.charAt(inputString.length() - 1);
    String mid = inputString.substring(1, inputString.length() - 1);
    return (firstChar == lastChar) && isPalindrome(mid);
    }
   }

}

public class PalindromeTest extends TestCase {
   
    public PalindromeTest( String testName )
    {
        super( testName );
    }

    public static Test suite()
    {
        return new TestSuite( PalindromeTest.class );
    }

    public void testPalindrome()
    {
    Palindrome palindromeTester = new Palindrome();
    assertTrue(palindromeTester.isPalindrome(""));
    }
}

The file structure of the the project is like this 

Add in the JaCoCO Plugin configuration into pom.xml as below to execute the JaCoCo prepare-agent and generate the report

<build>
  <plugins>
<plugin>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
<forkMode>once</forkMode>
<forkCount>1</forkCount>
<argLine>@{argLine} -Xmx1024m -XX:MaxPermSize=512m</argLine>
<reuseForks>false</reuseForks>
   </configuration>
</plugin>
<plugin>
   <groupId>org.jacoco</groupId>
   <artifactId>jacoco-maven-plugin</artifactId>
   <version>0.7.6.201602180812</version>
   <executions>
<execution>
<id>prepare-agent</id>
<goals>
           <goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
            <goal>report</goal>
</goals>
</execution>
   </executions>
</plugin>
</plugins>
</build>

After saving all the changes, run the maven command "mvn clean verify", The jacoco.exec file will be generated under target folder. And the report is generated under target/site/jacoco.


Open the index.html, the code coverage result shows up


Drill down to see the coverage for specific class. The red line are the ones haven't been covered and the green lines are the ones covered.

JaCoCo With Eclipse Plugin
You can use JaCoCo in Eclipse with zero configuration by using EclEmma Eclipse.
Install the EclEmma Java Code Coverage in Eclipse, you will get an icon as

 at your tool bar.


Click on the button and unit test will run automatically with code coverage reporting under "Coverage".



References:

  • http://www.baeldung.com/jacoco
  • http://www.eclemma.org/jacoco/trunk/doc/index.html




No comments :

Post a Comment