Saturday, August 20, 2016

Jacoco

JaCoCo is an open source toolkit for measuring and reporting Java code coverage. JaCoCo offers line and branch coverage. which requires instrumenting the source code, JaCoCo instruments the bytecode while running the code and offline also. To do this it runs as a Java agent, and can be configured to store the collected data in a file, or send it via TCP. Files from multiple runs or code parts can be merged easily. Unlike Cobertura and Emma it fully supports Java 7 and Java 8.

How to use it in Server Deployed Build - 


Please follow bellow given steps to achieve code coverage of sever deployed build-

a) Install Apache ANT

b) Download Jacoco jars from http://www.eclemma.org/jacoco/ . all the jars will be available inside lib folder of downloaded folder.

c) Create a specific folder named Jacoco (or any other name) in the same directory of Apache Tomcat.

d) Place all the Jacoco jars (specifically jacocoagent.jar, jacocoant.jar) inside above created folder.

e) Create two more folders inside Jacoco or above mentioned folder, named ST and reports.

     Folder Structure will be like - 
            
            Jacoco
             |--- ST
             |--- reports
             |--- jacocoagent.jar
             |--- jacocoant.jar


f) 
Now pass following JVM arguments through tomact, either by mentioning in catalina.bat (or catalina.sh) or by mentioning in setenv.bat (or setenv.sh) -

-javaagent:{path to jacocoagent.jar}=destfile={path to jacoco.exec},append=false

I mentioned these parameters in setenv.bat file in following way -

set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms256m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=1024m -javaagent:C:/Install/apache-tomcat-7.0.54/jacoco/jacocoagent.jar=destfile=C:/Install/apache-tomcat-7.0.54/jacoco/ST/jacoco.exec,append=false 

g) Now create and deploy war file of project inside webapps folder of tomcat. and start tomcat.

h) You will see that jacoco.exec file will get generated inside ST folder, but will be of size zero.

i) After going through the application in browser stop the tomcat, now check size of jacoco.exec file, it will have some size now.

j) Now have a look on build.xml file available with this tutorial. create a folder in your system in same location where your project is available, and copy this build.xml file inside that folder. and update following variables in the file according to their folder location-

src.dir - Source folder of original java code (.java files) which will be available inside your project.

result.dir - Path till your WEB-INF folder of deployed build folder (extracted inside webapps)

result.report.dir - Path to your created Jacoco/reports folder

result.exec.file - Path to your jacoco.exec file available inside ST folder.

k) Mention jacocoant.jar path inside taskdef tag in classpath tag in path variable.

l) After configuring build.xml file execute it using ant command.

m) Now check your reports folder of Jacoco.


The build.xml file will be like -

<?xml version="1.0" encoding="UTF-8"?>


<project name="Example Ant Build with JaCoCo" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">

<description>
 Example Ant build file that demonstrates how a JaCoCo coverage report
 can be itegrated into an existing build in three simple steps.
</description>

<property name="src.dir" location="./src" />
<property name="result.dir" location="C:/Install/apache-tomcat-7.0.54/webapps/bootstraptest/WEB-INF" />
<property name="result.classes.dir" location="${result.dir}/classes" />
<property name="result.report.dir" location="C:/Install/apache-tomcat-7.0.54/jacoco" />
<property name="result.exec.file" location="C:/Install/apache-tomcat-7.0.54/jacoco/ST/jacocoST.exec" />

<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="C:/Install/apache-tomcat-7.0.54/jacoco/jacocoant.jar" />
</taskdef>

<target name="report">
<!-- Step 3: Create coverage report -->
<jacoco:report>

<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>

<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>

<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>

<target name="rebuild" depends="report" />

</project>


No comments:

Post a Comment