Getting started with Cobertura – A Java Code Coverage Tool

0 0
Read Time:5 Minute, 23 Second

Cobertura a coverage tool. A coverage tool shows how many lines of code
are touched. In most cases you’ll use Cobertura to see how good your
regressions test are. You can also use it to see how many lines of code
are reached or to improve code that is accessed a lot.

Cobertura
will generate HTML or XML reports. Cobertura is meant to be used with
ant, but it also works with the command line and plugins are under
developement for Maven2 and Eclipse

This article will explain how to interpret the generated reports and how to use Cobertura in your projects.

Test reports

Let’s start with a test report and see what’s in it. A test report looks like this:


The
first column shows the package name, the second column how many classes
there are in the package. The third column is named Line coverage. Line
coverage is the amount of (useful) lines of code that are executed. A
useful line is a line of code where something happens (so no curly
braces or newlines).

The next column gives us information about
the branch coverage. Branch coverage is the coverage of the decision
points in your class (this can be a if/then/else loop. When your code
only reaches the first part of the if-loop and skips the else part
you’ll have 50% branch coverage). You’re probably thinking you’ll never
get a higher coverage than 50% with if/then/else loops. With Cobertura
it is possible to execute your code as many times as you want to test
all the branches you have.

The last column is the so called
McCabe cyclomatic code complexity. This is a way to determine how
complex your code is. One of the methods McCabe uses is measuring the
amount of decision points in your code. A higher rating means more
complex code.

 

A high coverage does not mean that your
code is free of bugs! It is very easy to write code that touches many
lines but doesn’t really do anything. Always keep in mind to write
useful tests and then Cobertura will be a useful tool.

This
code example shows the second part of the report. The numbers next to
the line numbers tell us how many times this line is exected. The lines
without a number are the so called ‘not useful’ lines I mentioned
earlier. When the line is colored red it means that this line is never
executed. In the example above this isn’t a bad thing because when this
exception is thrown it probably isn’t a fault in the java code.

You
can use this overview for two purposes. One is check lines that are
called many times. If you try to improve the effiencey of these lines
you will improve the efficience of your whole application. The othe
purpose is checking the coverage of your tests. If some parts of your
code are never reached you might want to consider writing a test for it.

Using cobertura

In the following examples I assume you’re using the directory structure of a Maven2 project and exectute all the commands in the root directory of your project.

cobertura-instrument

To
let Cobertura to see your compiled classes you have to instrument them.
Instrumenting is done by executing the following command: cobertura-instrument target/classes/. 

It
is possible to include more directories, some people think it is useful
to include unit test files. Jar’s can also be included.

Instrumenting the classes is nothing more than manipulating the java byte code (with ASM).
On every line of java code there are 3 lines added to the existing
classes to count things for your report. To communicate with Cobertura
the cobertura.jar file must be on your classpath.

Now it is time
to execute your classes. It usually means running all your JUnit tests,
but it basically can be anything (TestNG, no framework,  etc.). When
you’re classes are instrumented right you will see a message like
“Cobertura: Loaded information on 39 classes.”

Cobertura uses
a datafile (default is cobertura.ser) to generate the report. The .ser
file is generated after instrumenting the classes and modified after
executing the classes. (Note that the datafile parameter only defines
the location of the first .ser file). While executing the classes the
.ser file will end up in the directory where the classes are executed
(in Tomcat this is the /jakarta-tomcat-5.5.12/bin directory, with
maven2 it is the directory where the pom.xml is)

If you notice strange behaviour check your (sub)directories for .ser files (they should all be removed)

Don’t
forget to recompile your classes when you’re finished with Cobertura or
else Cobertura will be executed every time. It is possible to output
the instrumented classes to a user defined directory, but this is not
really handy with Maven. If you’re using Ant and JUnit it is done
automatically for you.

It is also possible to instrument the
classes of a webapp. The first problem you’ll encounter is that the
.ser file is not updated. This is done when you exit the servlet
container. Testing your webapp is useful to detect unused code or lines
that are executed many times. It is possible to write the .ser file
during executing, visit the Cobertura site for more information.

cobertura-report

We’re not finished yet. The report has to be generated. Execute the following command to generate a report:
cobertura-report –format html –destination c:\temp\cobertura –source src\main\java
Now there should be a nice site in c:\temp\cobertura

It is possible to include multiple source directories. You can do this by adding  –-source link\to\dir to the existing command.

Conclusion

Cobertura is a very nice tool that really helped me improve the quality of my code. It is nice to see the green bars grow .
I really look forward to the Maven2 and Eclipse plugin. But for now
this short list of commands will do the trick for any Maven2 project:

  • del *.ser
  • mvn clean
  • mvn test
  • cobertura-instrument target\classes\.
  • mvn test
  • cobertura-report –format html –destination c:\temp\cobertura –source src\main\java
  • mvn clean

Alternatives

Emma
Clover (commercial) Clover has plugins for Jdeveloper, Eclipse, Netbeans, IntelliJ IDEA and Jbuilder

Sources

http://www-128.ibm.com/developerworks/java/library/j-cobertura/

http://www-128.ibm.com/developerworks/java/library/j-cq01316/index.html?ca=drs


http://cobertura.sourceforge.net/index.html

About Post Author

Jeroen van Wilgenburg

Oracle Consultant at AMIS
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

13 thoughts on “Getting started with Cobertura – A Java Code Coverage Tool

  1. Hi all, thanx Jeroen for the reply….but it does not resolve my issue I am facing problem with cobertura coverage report generation.I am using Maven2.0.4 ,Eclipse 3.1, Jdk1.4.And while trying to generate the report after executing mvn site command getting the following error:

    [INFO] Error getting reports from the plugin ‘org.codehaus.mojo:cobertura-maven-
    plugin’: Unable to find the mojo ‘org.codehaus.mojo:cobertura-maven-plugin:2.0:c
    obertura’ in the plugin ‘org.codehaus.mojo:cobertura-maven-plugin’
    org/apache/maven/reporting/AbstractMavenReport

    If you could reply as soon as possible,any help from your side will be appreciated…..

  2. Hi all, thanx Jeroen for the reply….but it does not resolve my issue I am facing problem with cobertura coverage report generation.I am using Maven2.0.4 ,Eclipse 3.1, Jdk1.4.And while trying to generate the report after executing mvn site command getting the following error:

    [INFO] Error getting reports from the plugin ‘org.codehaus.mojo:cobertura-maven-
    plugin’: Unable to find the mojo ‘org.codehaus.mojo:cobertura-maven-plugin:2.0:c
    obertura’ in the plugin ‘org.codehaus.mojo:cobertura-maven-plugin’
    org/apache/maven/reporting/AbstractMavenReport

    If you could reply as soon as possible,any help from your side will be appreciated.

  3. The file cobertura-instrument is probably not available on your path. In windows you have to put the directory where Cobertura is installed in the list at the PATH entry. Right click my computer, properties, advanced, enviroment variables and add the cobertura directory at the variable PATH at the user section (when there is no PATH, create it)

  4. cobertura-instrument target\classes\.

    cobertura-report –format html –destination c:\temp\cobertura –source src\main\java

    Can anybody please, help me out with this?
    These commands are not working for me.result of execution goes like:
    ‘cobertura-report’ is not recognized as an internal or external command,
    operable program or batch file.

    ‘cobertura-instrument’ is not recognized as an internal or external command,
    operable program or batch file.

  5. Hi Jeroen,
    That plugin supports cobertura 1.7.
    I want a plugin which supports cobertura 1.8.
    Thanks,
    Sachi

  6. Hi Piyush,
    I am trying to locate the cobertura-maven-plugin for maven 2 and cobertura 1.8.
    Where did you get that plugin?
    Thanks,
    Sachi

  7. Hi All,
    I’m using Maven 2.0.4 and Coubertura 1.8. I’ve no issues producing reports when executing the steps as mentioned in the conclusion. But When I use Maven and do mvn cobertura:cobertura, I get a Null Pointer Exception. There is no problem in instrumenting the classes thou’.
    For your reference I’m providing the stack trace.

    [ERROR] Exception in thread “main” java.lang.NullPointerException
    at net.sourceforge.cobertura.coveragedata.ProjectData.getSourceFiles(Pro
    jectData.java:105)
    at net.sourceforge.cobertura.reporting.html.HTMLReport.generateSourceFil
    eList(HTMLReport.java:167)
    at net.sourceforge.cobertura.reporting.html.HTMLReport.generateSourceFil
    eLists(HTMLReport.java:150)
    at net.sourceforge.cobertura.reporting.html.HTMLReport.(HTMLReport
    .java:79)
    at net.sourceforge.cobertura.reporting.Main.parseArguments(Main.java:100
    )
    at net.sourceforge.cobertura.reporting.Main.main(Main.java:161)

    Can anyone please guide me as to what I’m missing.

    Thanks in advance,
    Piyush

  8. I think there’s no plugin for Eclipse, but there is for JClover and both applications are quite similar, so it shouldn’t be too difficult to convert the JClover plugin I guess.

    What exactly do you mean with create your own instrument classes? Instrumenting a class is nothing more than some byte code manipulation of compiled classes.

  9. Any one of you have idea that on the basis of what we can create our own instrument classes or is there any eclipse plugin for j-cobertura and eclipse.

    Thanks & Regards
    Richa

  10. Release 1.8 of Cobertura just was published: After 4 long months we’ve released Cobertura 1.8! Lots of good changes in this release: show the total number of lines and branches in the HTML report, support for Groovy, allow multiple JVMs to cleanly write to the same data file, and more robust instrumentation of archives (and even archives inside of archives!). See
    http://cobertura.sourceforge.net/

Comments are closed.

Next Post

Construct a "Save" URL syntax for the Oracle Reports Server

For a customer had to create an Oracle Forms functionality which would need to make several requests to the Oracle Report Server. The customer wanted me to generate a report to the screen,  print the same report to the OS and enter the report as a BLOB type in the […]
%d bloggers like this: