Enterprise Java

Covering your tests with Cobertura, JUnit, HSQLDB, JPA

Hello, how are you?

Let us talk today about a very useful tool named “Cobertura”. This framework has the same functions of the Emma framework that we saw in another post.

The major difference between Cobertura and Emma is that Cobertura displays the resume page with graphics.

If you want to see the other topics about this subject click on the links: Coverage of tests with JUnit Ant and Emma // JUnit with HSQLDB, JPA and Hibernate // TDD – First Steps.

I will use the same code from this post (JUnit with HSQLDB, JPA and Hibernate); if you want to set up an environment to run the code you can follow the steps that you will find there (you will find the source code to download at the end of this article).
I am not a skilful Ant user and you may see some code and think “this script is not good”. Fell free to give me ideas of how to “upgrade” the ant code. ;)

Let us do the download of the Cobertura libraries – Download1.9.4.1

After the download finishes put the jar files (/cobertura.jar, /lib/* that includes asm, jakarta, log4j) inside the lib folder of our project.

In the root source of our project create a file named “build.xml” with the following code (the created file needs to be in the root of your project or your project will not work):

<project name="Cobertura Coverage" basedir=".">
    <!--  Project Source  Code -->
    <property name="src.dir" value="src" />
    <property name="build.dir" value="bin" />
    <property name="teste.dir" value="src/test" />
    <property name="lib.dir" value="lib" />
    <property name="report.dir" value="cobertura" />
    <!-- Project classpath -->
    <path id="project.classpath">
        <pathelement location="${bin.dir}" />
        <fileset dir="${lib.dir}">
            <include name="*.jar" />
    <!-- Tested Class -->
    <property name="DogFacadeTest" value="test.com.facade.DogFacadeTest" />

In the code above we are creating the paths to the source codes and to the libraries. Let us create a task to delete the files generated by the Cobertura and the compilated Java source code.

<!-- Clears the paths -->
<target name="01-CleannUp" description="Remove all generated files.">
    <delete dir="${build.dir}" />
    <delete file="cobertura.ser" />
    <delete dir="${report.dir}" />
    <mkdir dir="${build.dir}" />
    <mkdir dir="${report.dir}" />

To compile our source code, add the code bellow and run the task (be sure that the code will compile):

<!-- Compiles the Java code -->
<target name="02-Compile" depends="01-CleannUp" description="invoke compiler">
    <javac debug="true" debuglevel="vars,lines,source" srcdir="${src.dir}" destdir="${build.dir}">
        <classpath refid="project.classpath" />
    <copy file="${src.dir}/META-INF/persistence.xml" todir="${build.dir}/META-INF" />

Let us setup the Cobertura so it may instrument the tests class and get the environment ready:

<!-- Cobertura configs -->
<property name="cobertura.instrumented-classes.dir" value="${report.dir}/instrumented-classes" />
<property name="cobertura.data.file" value="cobertura.ser" />
<path id="cobertura.classpath">
    <fileset dir="${lib.dir}" includes="/*.jar" />
<!-- Points to the cobertura jar -->
<taskdef classpath="${lib.dir}/cobertura.jar" resource="tasks.properties" classpathref="cobertura.classpath" />
<!-- Instruments the classes -->
<target name="03-Instrument" depends="02-Compile">
    <delete quiet="false" failonerror="false">
        <fileset dir="${cobertura.instrumented-classes.dir}" />
    <delete file="${cobertura.data.file}" />
    <cobertura-instrument todir="${cobertura.instrumented-classes.dir}">
        <fileset dir="${build.dir}">
            <include name="**/*.class" />
            <exclude name="**/*Test.class" />
    <copy todir="${cobertura.instrumented-classes.dir}">
        <fileset dir="${src.dir}" casesensitive="yes">
            <patternset id="resources.ps" />

Add the code bellow and you will be able to execute the tests with the JUnit through the ant:

<!-- Set up the instrumented classes path -->
<path id="cover-test.classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar" />
    <pathelement location="${cobertura.instrumented-classes.dir}" />
    <pathelement location="${build.dir}" />
<!-- Run the JUnit test -->
<target name="04-RunTest" depends="03-Instrument" >
    <junit printsummary="yes" haltonerror="no" haltonfailure="no"  fork="yes">
            <fileset dir="${build.dir}" includes="**/*Test.class" />
        <classpath refid="cover-test.classpath" />
    <delete file="transaction.log" />

As our last action, let us create the report using the Cobertura. Add the code bellow to your “build.xml” and execute the task:

<!-- Creates the Cobertura report -->
<target name="00-CreateReport" depends="04-RunTest">
    <cobertura-report srcdir="${cobertura.data.file}" destdir="${report.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.java" />
    <delete dir="${report.dir}/instrumented-classes" />
    <delete file="cobertura.ser"  />

Update your Eclipse project (press F5 in the project) and you will see that the reports were created with success. Open the “index.html” file that it is inside the “cobertura” folder.

The Cobertura framework helps us calculating how many times we should test a method. As homework write the Dog equals method, create the report so you may see that your equals is not covered. Create your tests and run the report again.

You can download the source code from our project in here.

If you have any doubts or questions, just write it bellow.

See you soon. \o_

Reference: Covering your tests with Cobertura, JUnit, HSQLDB, JPA from our JCG partner Hebert Coelho at the uaiHebert blog.

Hebert Coelho

Senior Java Development, with 4 certifications and a published book about JSF (portuguese only). Founder of the blog uaiHebert.com visited from more than 170 different countries.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

I imported this project.
Run from command line.
But in index.html I see, that Coverage if 0 !
Help please, why it’s happened

8 years ago


Back to top button