Java Code Coverage With Cobertura and Jenkins

Introduction

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.

Cobertura

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:

ant

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

$/Users/noahgift/Downloads/cobertura-1.9.4.1/examples/basic ant test
Buildfile: /Users/noahgift/Downloads/cobertura-1.9.4.1/examples/basic/build.xml
init:
compile:
[javac] /Users/noahgift/Downloads/cobertura-1.9.4.1/examples/basic/build.xml:36: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
test:
BUILD FAILED
/Users/noahgift/Downloads/cobertura-1.9.4.1/examples/basic/build.xml:72: The <classpath> for <junit> must include junit.jar if not in Ant's own classpath
Total time: 0 seconds
$/Users/noahgift/Downloads/cobertura-1.9.4.1/examples/basic

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.

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

Jenkins

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:

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

**/reports/cobertura-xml/coverage.xml

4. build it.

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

Conclusion

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.