Java Code Coverage With Cobertura and Jenkins
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-188.8.131.52/examples/basic ant test
[javac] /Users/noahgift/Downloads/cobertura-184.108.40.206/examples/basic/build.xml:36: warning: ‘includeantruntime’ was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
/Users/noahgift/Downloads/cobertura-220.127.116.11/examples/basic/build.xml:72: 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="build.properties" />
<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: https://wiki.jenkins-ci.org/display/JENKINS/Cobertura+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: https://wiki.jenkins-ci.org/display/JENKINS/Cobertura+Plugin
Measuring Code Coverage With Cobertura: https://www.ibm.com/developerworks/java/library/j-cobertura/
The Loggly and SolarWinds trademarks, service marks, and logos are the exclusive property of SolarWinds Worldwide, LLC or its affiliates. All other trademarks are the property of their respective owners.
Hoover J. Beaver