This is probably a rather simple question, but I'm at a loss...
I have an if statement like the following:
if(TheEnum.A.equals(myEnum) || TheEnum.B.equals(myEnum))
TheEnum
can be A
, B
, C
, ... G
(more than just 4 options).
JaCoCo (SONAR) tells me that there are four conditions I can cover here. Which ones are those? Isn't the entire set I can test for in this instance essentially
if(true || not_evaluated) => true
if(false || true) => true
if(false || false) => false
I'm pretty sure I can't specifically test for
if(true || true)
or
if(true || false)
,
as short circuit evaluation won't get that far...?
If so, what is the forth option JaCoCo/Sonar wants me to test for?
Branches (C1 Coverage) JaCoCo also calculates branch coverage for all if and switch statements. This metric counts the total number of such branches in a method and determines the number of executed or missed branches. Branch coverage is always available, even in absence of debug information in the class files.
To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.
To efficiently cover all 6 branches in this case, the test function must be called no less than 4 times to achieve 100% branch coverage. Save this answer. Show activity on this post.
What is Statement Coverage? It is one type of white box testing technique that ensures that all the statements of the source code are executed at least once. It covers all the paths, lines, and statements of a source code.
You are right, this code is short-circuiting. It's compiled into bytecode roughly like this (assuming Java has goto):
if(TheEnum.A.equals(myEnum)) goto ok;
if(!TheEnum.B.equals(myEnum)) goto end;
ok:
// body of if statement
end:
So as JaCoCo analyzes the bytecode, from its point of view you have the two independent checks: first if
and second if
, which generate four possible branches. You may consider this as a JaCoCo bug, but I guess it's not very easy to fix this robustly and it is not very disturbing, so you can live with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With