JCov Tutorial | Coverage tool installation and simple use case

This tutorial is intended to help on how to build JCov and demonstrate a simple use case. This tutorial is for Ubuntu.

First Step in to install Java, in my case I have installed Java 8. Below are the command to install.

  • sudo add-apt-repository ppa:openjdk-r/ppa
  • sudo apt-get update
  • sudo apt-get install openjdk-8-jdk
  • sudo apt-get install openjdk-8-jre

Next step is to install apache ant and mercurial. Use the below commands to install:

  • sudo apt-get install ant
  • sudo apt-get install mercurial

Next download asm framework and JT Harness from the below url, these two are required to build JCov

Asm framework: wget http://download.forge.ow2.org/asm/asm-5.0.1-bin.zip

JT Harness: wget https://adopt-openjdk.ci.cloudbees.com/job/jtharness/lastSuccessfulBuild/artifact/jtharness-4.6.tar.gz

Extract asm-5.0.1-bin.zip and jtharness-4.6.tar.gz

In my setup Asm framework and JT Harness were downloaded and extracted at /home/test therefore my path for Asm framework is /home/test/asm-5.0.1/lib/all/asm-all-5.0.1.jar and for JT Harness /home/test/javatest/lib/javatest.jar

Next we need to clone JCov repo, use the below command to clone the repo

Now we have installed all the dependencies and ready to build JCov. Go to jcov/build directory and use the below commands. Don’t forget to replace your path for Asm framework and JT Harness in the command.

  • ant clean
  • ant -v -f build.xml -Dasmjar5=/home/test/asm-5.0.1/lib/all/asm-all-5.0.1.jar -Djavatestjar=/home/test/jtharness/lib/javatest.jar

 

This should build JCov, now lets test JCov with a sample use case, save the below code as Hello.java:

public class Hello {

public static void main(String[] argv) {

System.out.println(“Hello World”);

}

}

  •    Compile using java

    javac Hello.java

  •    Instrument it

    java -jar /home/test/jcov/JCOV_BUILD/jcov_3.0/jcov.jar Instr Hello.class

    This step should generate template.xml file

  •    Run it

    java -classpath .:/home/test/jcov/JCOV_BUILD/jcov_3.0/jcov_file_saver.jar Hello

    Hello can be replace with the test case class. This step should generate result.xml

  •    Generate report

    java -jar /home/test/jcov/JCOV_BUILD/jcov_3.0/jcov.jar RepGen -o report result.xml

Report should be created in report folder. Open it using the below command:

    xdg-open report/index.html

In a similar way jar files can also be instrumented, in the instrumentation step just replace Hello.class file with the desired jar. If you want code highlighting available then you may need to pass -src flag during report generation. Below is an example:

java -jar /home/test/jcov/JCOV_BUILD/jcov_3.0/jcov.jar RepGen -src /home/test/workspace/Hello/src/ -o report result.xml

301 Comments

  1. Works fine with class files but not with jar. can u please help me with it giving an example or simply telling me why it didn’t work?
    you can see the error I encountered here : “http://stackoverflow.com/q/41242330/6790957”

    • later i switched to jacoco as i wanted to intrument jars on the fly.
      may be today in the evening or tomorrow i will write a post on using jacoco.

Leave a Reply