测试我的代码时我遇到了无法解释的事情。使用 eclemma
检查代码覆盖率我找到了 for-loop
的标题以黄色突出显示,消息内容为 "1 of 2 branches missing"
.
代码行如下:
for (int i = maxIdx; i >= 0; i--) {
循环体突出显示为已覆盖(并且实际执行),以及前面和后面的语句,并且该方法在所有可能的条件下都可以正常工作。其他for-loops
的标题据我所知,只有在循环体从未执行过的情况下,才会以黄色突出显示并显示相同的消息。
这则消息的意义是什么?缺少哪个分支?
最佳答案
方法如下for
表单循环
for (ForInit; ForCondition; ForUpdate)
Body
执行:
-
ForInit
已执行 -
ForCondition
被评估- 当
false
,然后Body
不执行,循环后继续执行 - 当
true
,然后Body
被执行,ForUpdate
被执行并从步骤 2 继续执行
- 当
“2个分支”对应ForCondition
的上述两个选项.
“2 个分支中的 1 个缺失”意味着仅发生了这些选项之一,要么是第一个,要么是第二个。
<小时/>没有 complete example 包括循环体,很难回答您的其他问题
But strange -- why then other loops that always executed at least once are green?
Yet it's rather strange -- why other loops are always green?
但是考虑到Body
循环已执行,可能是 Body
中的循环退出之前ForCondition
计算结果为 false
.
例如,使用 EclEmma 3.1.1 附带的最新版 Eclipse IDE for Java 2018-12:
也许您的其他循环中没有这样的导出:
这也能解释
Running this code with an empty
StringBuilder
paints it green.
和
Adding an artificially created situation with an empty
StringBuilder
(that's impossible in reality) colors the loop in green.
因为添加了 ForCondition
时的情况计算结果为false
在执行 Body
之前:
关于java - Eclemma:for 循环中缺少分支——这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53959614/