1

I'm currently in the process of becoming a TDD hacker. As part of this I feel that a code coverage report like the one Cobertura generates is a essential piece of tool.

But, my code coverage is somewhat bloated as the following code doesn't get covered correctly:

if(logger.isLoggable(Level.INFO)) {
    logger.info("blah blah blah");
}

I'm running the report within Jenkins and Maven (don't know is that can be a problem?). isLoggable(Level.INFO) is (in my case) always true thus it enters and logs. I'd like to be able to either skip the part or be able to set isLoggable(Level.XX) to true AND false.

Any ideas, hints etc? Maybe something I have overlooked, everything is much appreciated! :)

[edit] Seems like a bug in the Maven cobertura plugin which should be fixed in 2.6: http://jira.codehaus.org/browse/MCOBERTURA-52 BUT 2.6 haven't been released, so I'll just have to wait... [/edit]

sunlock
  • 212
  • 5
  • 17

2 Answers2

1

When you instrument the classes you can ignore certain methods/calls. The report is a step after that.

With ant this is done like this (example from the docs). Maven might have the same options, but this depends on the maven plugin you use.:

<cobertura-instrument todir="${instrumented.dir}">
    <ignore regex="java.util.logging.*" />
    <fileset dir="${classes.dir}">
        ...
    </fileset>
</cobertura-instrument>

You simply set an ignore regex for the packages you want to have excluded from instrumentation, this way they won't show up in the reports.

With Maven this should work:

<instrumentation>
    <ignores>
       <ignore>java.util.logging.*</ignore>
    </ignores>
</instrumentation>
oers
  • 18,436
  • 13
  • 66
  • 75
  • I'm using maven (http://mojo.codehaus.org/cobertura-maven-plugin/) and tried adding an ignore, but nothing happens, exclude works fine. Any ideas? Also, I'm using java.util.logging (not log4j). I know, but I'm not going to change it right now (unless that is the only way I can "fix" this). – sunlock Jul 13 '12 at 10:50
  • I changed the answer to java.util.logging. I know that the ant way works therefore the maven way should work too, but I never used it with maven. – oers Jul 13 '12 at 11:01
  • That is what I have... but it doesn't work - if I add excludes it excludes the source files (thus the instrumentation part works, only seems like ignore doesn't)... – sunlock Jul 13 '12 at 11:52
  • did you -clean- before retrying? – oers Jul 13 '12 at 12:36
  • Yeah, already have my conf in . After doing a lot of digging I have found that v2.2 of the plugin works with ignore, but f*** everything else - v2.3 and new works fine but f***s with ignore :( – sunlock Jul 13 '12 at 14:00
0

There is nice article explaining in details how you can exclude logging calls from cobertura instrumentation. http://fahdshariff.blogspot.gr/2010/01/cobertura-ignore-logger-calls.html and a similar question posted here: Exclude methods from code coverage with Cobertura

I think you might find useful both of these links

Community
  • 1
  • 1
ppapapetrou
  • 1,653
  • 9
  • 13
  • I have seen the blog and question, but it doesn't seem to skip them. I'm currently running with version 2.5.1, but if I roll back to 2.2 (as on the blog post) it works... A bug perhaps? – sunlock Jul 13 '12 at 12:36