Grails / Cobertura report - conditional coverage doesn't make sense
Asked Answered
E

3

10

What could cause something like this:

coverage report fragment

The line def result shows only 2/6 branches covered, even though it has NO BRANCHES, while the following line, which actually has a conditional, is ok.

What's going on? I verified that the cobertura.ser is getting cleaned up between builds.

Embargo answered 20/9, 2013 at 17:44 Comment(2)
Is result down the lane?Anvers
yes, result is assigned later on.Embargo
C
7

any idea where I can get the source for that JAR?

jar and souce code for disableOptimizationsTransformation

Also - any idea how to include that JAR on classpath ONLY for the test-app build phase?

// Remove the jar before the war is bundled
grails.war.resources = { stagingDir ->
  delete(file:"${stagingDir}/WEB-INF/lib/DisableOptimizationsTransformation-0.1-SNAPSHOT.jar")
}

from other post here

Choragus answered 3/1, 2014 at 18:53 Comment(2)
Will that really only apply to the test-app build phase?Sheathbill
Thanks! And to make use of this jar in Gradle, I found this useful: https://mcmap.net/q/1163492/-how-to-use-gradle-to-link-a-jar-in-libTabernacle
M
5

Same discussion also appeared in the official forum, see Branch coverage issues .

@rgarcia gave an excellent little tool jar to disable the AST optimization so that Cobertura is able to compute the coverage correctly.

To use the jar, just put it in your myapp\lib folder and then test-app -coverage:)

Mosstrooper answered 2/1, 2014 at 16:58 Comment(1)
any idea where I can get the source for that JAR? Also - any idea how to include that JAR on classpath ONLY for the test-app build phase?Embargo
F
4

I've noticed the same thing in our grails projects - I think this is caused by the "optimization" branches the groovy compiler creates.

For example - this code

def deleteSomething(params) {
   def result
   if(params.something && params.somethingelse)
      result = "something"
   else result = "something else"
}

looks like this when compiled

public Object deleteSomething(Object params)
{
   CallSite[] arrayOfCallSite = $getCallSiteArray(); Object result = null; if ((!BytecodeInterface8.isOrigZ()) || (__$stMC) || (BytecodeInterface8.disabledStandardMetaClass())) {
      if (((DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[2].callGetProperty(params))) && (DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[3].callGetProperty(params))) ? 1 : 0) != 0) {
         String str1 = "something"; result = str1; return str1; } else {
         String str2 = "something else"; result = str2; return str2;
      }
   }
   else if (((DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[4].callGetProperty(params))) && (DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[5].callGetProperty(params))) ? 1 : 0) != 0) {
      String str3 = "something"; result = str3; return str3; } else {
      String str4 = "something else"; result = str4; return str4; } return null;
}

More discussion here.

Fruity answered 20/9, 2013 at 19:17 Comment(3)
Plausible explanation - so how do I fix it in a Grails app?Embargo
Thanks for your explanation. This confuses me too:(Mosstrooper
@wrschneider99 After some survey, I found a solution, please check my answer.Mosstrooper

© 2022 - 2024 — McMap. All rights reserved.