Eclemma says 1 of 4 branches not covered, but which branch is it?

ⅰ亾dé卋堺 提交于 2019-12-21 03:29:09

问题


Is there a simple way to tell which branch I am missing? I.e. I have some code like this:

if (x || y) {
    // do stuff
}

In the coverage highlighting there is a yellow dot in Eclipse that says:

1 of 4 branches missed

but I would like to know which branch is missing.


回答1:


An open issue on the github repo for Eclemma's parent, jacoco, suggests that such a feature would actually be a bit difficult to include.

However, even without an Eclemma feature, if the goal is just to figure out the branches missed in a specific case, you could instrument your code to keep track. The simplest example is old fashioned print statements:

if (x || y) {
    System.out.println("BRANCH: " + x + ", " + y);
    // Do stuff
}

Then look at the output and see what branches you actually hit (e.g. java ... | grep "BRANCH:" | sort | uniq). (not terribly satisfying, I know.)




回答2:


What can x and y be?

  • true || true is true (Not covered because of JVM optimization: if the first condition is true, the second won't be evaluated due to short circuit evaluation)
  • false || true is true
  • true || false is true
  • false || false is false



回答3:


The answer is true|| true was not covered.

This is because once the JVM has found the first condition to be true, then it won't run the second condition (it's optimized) which means that portion of the code is never run.

As Maroun said, 3 out of 4 branches will allow the conditional to pass. If you're still worried about code coverage, you can refactor the conditional to be a && instead of a ||.

(x || y) is the same as (!(!x && !y)) and this will allow you to test all conditions since there are only three branches now.

The original form of the conditional is often seen in guard statements:

if (obj == null || obj.hasError())
{
    throw new RuntimeException();
}

This will never allow you to check if obj is null AND has an error because it will throw a Null Pointer Exception.

If code coverage is important, then just use this form:

if (!(obj != null && !obj.hasError()))
{
    throw new RuntimeException();
}



回答4:


There is a quite easy woarkaround - just put each logic predicate in separate line, like this:

if (x 
    || y) {
    System.out.println("BRANCH: " + x + ", " + y);
    // Do stuff
}

Now when you'll run analisys, the marker should point directly on branch that is missed. After you'll cover it up, you can reformat your code the right way.

HTH




回答5:


There are likely implicit branches either from nested statements within the if block or statements from expanding your x or y predicates.

Read this: http://emma.sourceforge.net/faq.html#q.fractional.examples




回答6:


What will be the result of below expressions ?

  • true || true is true
  • true || false is true
  • false || true is true
  • false || false is false

If you notice in 1st and 2nd case when you have 1st parameter as TRUE, you don't need to check 2nd parameter.

  • true && true is true
  • true && false is false
  • false && true is false
  • false && false is false

Or if you see scenarios with AND operator, if first parameter is FALSE you never need to check for 2nd parameter.

And hence you are never able to check 1 branch out of 4 branches.



来源:https://stackoverflow.com/questions/15493094/eclemma-says-1-of-4-branches-not-covered-but-which-branch-is-it

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