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

江枫思渺然 提交于 2019-11-30 08:25:25

问题


What could cause something like this:

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.


回答1:


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




回答2:


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:)




回答3:


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.



来源:https://stackoverflow.com/questions/18922826/grails-cobertura-report-conditional-coverage-doesnt-make-sense

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!