Blog Development

Java Code Coverage With Cobertura and Jenkins

By Hoover J. Beaver 29 Nov 2011


Writing software is tough, tools and processes that reduce the difficulty are always welcome. Code Coverage is an interesting developer tool because it tells you how much you don’t know. Not having a test for a particular piece of code doesn’t mean that it doesn’t work, it just means you aren’t as sure as you could be that it works. In this article, I will be covering getting the open source, Java Code Coverage tool, Cobertura, working with Ant, Jenkins and Github.


Since getting continuous integration working in a particular language can be complicated, it is a best practice to break the problem down into discreet chunks. Fortunately, Cobertura makes this easy, because the source code comes with an example ant project. You can download a binary distribution here. Inside of the download will be a relative path ..examples/basic. If you cd into that directory, you can generate a code coverage report on a sample Java project by typing:


If you don’t have junit installed, you will get output like the below, on my OS X Lion laptop:

$/Users/noahgift/Downloads/cobertura- ant test
Buildfile: /Users/noahgift/Downloads/cobertura-


[javac] /Users/noahgift/Downloads/cobertura- warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds


/Users/noahgift/Downloads/cobertura- The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

Total time: 0 seconds

If you run ant clean it will remove the reports directory, which contains, old reports. One tricky bit with running the example, is that the build.xml file is set to look for cobertura, and the cobertura lib directory, in a relative path two directories above.

<property file="" />
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />

This can be tricky if you check in only the examples directory into your git repository and don’t have cobertura installed properly. What will happen then, is that the examples won’t build properly and it will be tricky to figure out. If you find yourself in this situation, one easy hack is to simply place the cobertura jar files, including the dependencies in the lib, inside of your ant home. On Ubuntu linux this is/usr/share/ant/lib


With the basic Cobertura configuration out of the way, the next thing to do is to create a throwaway git repository, on github or your own server. Next you will want to check in the whole Cobertura binary distribution that you download from their site, or only check in the examples root, and install Cobertura properly on your build server. Inside of jenkins you will need to do the following things:

1. Point Jenkins at your git repository.

2. Install the Cobertura jenkins plugin:

3. Configure the reports directory correctly. Because I only checked in the examples directory, my reports configuration looked like this:


4. build it.

Here is a screenshot of what it looks like when Jenkins builds the example project with code coverage:



If you were able to follow along at home, and have Jenkins automatically building the example project with code coverage, then the next step is to convert this knowledge to your own code base. One problem I had when getting Cobertura working with my code base was configuring the junit stanza properly to fork. Finally, an even simpler way to get code coverage cooking for your Java code base is to install the Eclipse plugin eCobertura. If you are really stuck getting things to work, this is a nice easy win. I hope you enjoyed the article, and if you have any Cobertura tricks, I would love to hear about them.


Cobertura Plugin Jenkins:




Measuring Code Coverage With Cobertura:


Hoover J. Beaver

Hoover J. Beaver

Share Your Thoughts