Branch you say? What branch?

January 27, 2008 – 8:46 pm

When I use Cobertura to instrument my Grails Controllers, it doesn’t seem to accurately report on the branch coverage. For instance, the index closure in my controller looks like this:

1
2
3
def index = {
	redirect(action:list,params:params)
}

The tests look like this:

1
2
3
4
5
6
7
8
9
10
void testIndex(){
	stateController.index()
	assertEquals('/state/list', stateController.response.redirectedUrl)
}
 
void testIndexWithParams(){
	stateController.params.max=10
	stateController.index()
	assertEquals('/state/list?max=10', stateController.response.redirectedUrl)
}

I don’t believe there are actually two possible branches to that little line of code with the redirect (line 2 in the first code example), but Cobertura reports only 1 of 2 branches is covered. What do you think is the elusive second branch? Apparently I’m not the only one with that question – I found this post on the Groovy users mailing list, but no solution.

In fact, the entire class apparently has some issues with the branch coverage:

For me this may not be a huge deal since I’m mostly using this report as a guide to highlight areas of code that aren’t covered at all by tests. But it would be nice to not have simple lines of code (where branch coverage should really be N/A) highlighted in red on the report.

If you’re interested, here are the Controller and the Test – I think it should be pretty close to 100% covered for both line and branch.

  1. 5 Responses to “Branch you say? What branch?”

  2. Out of curiosity, did you try other solutions like Clover and EMMA? I’d be curious to see if the other coverage tool report the same kind of things?

    By Guillaume Laforge on Jan 28, 2008

  3. That’s a great idea Guillaume – I just started with Cobertura because I was already familiar with it. I’ll try Emma when I have a chance, and maybe Clover after that (if I win the lottery!).

    By Mike on Jan 28, 2008

  4. Have you posted this issue to the cobertura devs? They may be able to give advice or fix it.
    (I would also profit from that fix ;-) )

    regards
    Dierk

    By Dierk König on Jan 28, 2008

  5. Good idea Dierk: http://www.nabble.com/question-about-covering-a-throw-statement-td11966841.html#a15136739

    By Mike on Jan 28, 2008

  6. Clover won’t work as far as I can tell, because it processes the Java files before compilation, rather than processing the class files afterwards. I also heard (second-hand) from a Clover consultant that it would not work with Grails.

    By Peter on Feb 12, 2008

Sorry, comments for this entry are closed at this time.