Log Management and Analytics

Explore the full capabilities of Log Management and Analytics powered by SolarWinds Loggly

View Product Info

FEATURES

Infrastructure Monitoring Powered by SolarWinds AppOptics

Instant visibility into servers, virtual hosts, and containerized environments

View Infrastructure Monitoring Info

Application Performance Monitoring Powered by SolarWinds AppOptics

Comprehensive, full-stack visibility, and troubleshooting

View Application Performance Monitoring Info

Digital Experience Monitoring Powered by SolarWinds Pingdom

Make your websites faster and more reliable with easy-to-use web performance and digital experience monitoring

View Digital Experience Monitoring Info

Blog Development

Java Code Coverage With Cobertura and Jenkins

By Hoover J. Beaver 29 Nov 2011

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:

[php]
$/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
[/php]

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.

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

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:

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:

**/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:

jenkinscodecoverage

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.

Ant: https://ant.apache.org/

Cobertura Plugin Jenkins: https://wiki.jenkins-ci.org/display/JENKINS/Cobertura+Plugin

Jenkins: https://jenkins-ci.org/

Github: https://github.com/

Cobertura: https://cobertura.sourceforge.net/

Measuring Code Coverage With Cobertura: https://www.ibm.com/developerworks/java/library/j-cobertura/

eCobertura: https://ecobertura.johoop.de/

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

Hoover J. Beaver