Accurate branch reports with Grails and Cobertura _1.8_

January 31, 2008 – 8:35 pm

I’m trying to figure out why Cobertura coverage reports on my Groovy and Grails code don’t show the branch coverage that I expect they should. I noticed that at some point in the past, Cobertura did show a correct branch report. So I tried the grails code-coverage plugin with Cobertura version 1.8:

100% Branch Coverage

TADA! Now the reports are showing the coverage I expect. I know Cobertura (version 1.9) improved on the way it calculates branch coverage (see the news item from 5 June 2007 on the Cobertura website) but for my purposes, Cobertura 1.8 gives me more of the information that I need. I’m mostly concerned with finding code that isn’t tested at all, and that’s hard to do when almost every line of code is highlighted in red like version 1.9 was doing:

Branch Coverage on StateController

So for now I’ve updated the Grails code-coverage plugin to utilize Cobertura 1.8. I can live without the new features that 1.9 offers and in the meantime I’ll work with the cobertura-devel folks to see if there’s something that we can improve for a future release.

  1. 6 Responses to “Accurate branch reports with Grails and Cobertura _1.8_”

  2. Hey Mike,

    Great job on this plugin. Cobertura has been my coverage tool of choice for a few years and now it is crazy-easy to use in my Grails projects. Hopefully the good folks on the cobertura-devel team can address this need.

    By Zan Thrash on Feb 1, 2008

  3. The plugin is great, nice work. Do you know of any way to keep the Cobertura-related JAR files out of the WAR file since they’re really only needed to analyze coverage?

    By Jeff on Feb 4, 2008

  4. Hi Mike,

    First of all, thanks for writing the plugin - very nice! I’m currently integrating it with a client’s project and have a patch or two. Since you’re already using the Grails plugin repository, I have set up a component in the Grails plugins JIRA:

    http://jira.codehaus.org/browse/GRAILSPLUGINS

    called ‘Grails-CodeCoverage’. I’m currently configured as the component lead, but if you let me know what your JIRA username is I’ll change that.

    By Peter on Feb 12, 2008

  5. Jeff - check out Dmitriy’s post regarding excluding JARs from the WAR file. I’ll try to figure out a way to do this as part of the plugin install, but in the meantime adding the following to your Config.groovy should do the trick:

    grails.war.resources = {stagingDir ->
    	delete(file: "$stagingDir/WEB-INF/lib/asm-2.2.1.jar")
    	delete(file: "$stagingDir/WEB-INF/lib/cobertura.jar")
    	delete(file: "$stagingDir/WEB-INF/lib/jakarta-oro-2.0.8.jar")
    	delete(file: "$stagingDir/WEB-INF/lib/log4j-1.2.9.jar")
    }

    By Mike on Feb 12, 2008

  6. Mike - Thanks for pointing me to the tip on excluding JARs from the WAR file. My only concern with the suggested approach is that I may be deleting dependencies of other libraries without realizing it. For instance, there may be many components in my application that use log4j-1.2.9.jar.

    I’m working through a couple of ideas to keep the JAR files from ever making it to the staging directory in the first place. I’ll let you know if I find something that works.

    By Jeff on Feb 15, 2008

  7. Okay, I figured out a way to keep the Cobertura-related JAR files from ever making it to the staging directory without breaking the code coverage. I’d like to share the changes if you don’t mind contacting me.

    By Jeff on Feb 15, 2008

Post a Comment