用逻辑或(||)测试if语句的覆盖率-由于Java的短路,JaCoCo要我覆盖的第四个条件是什么? [英] Test coverage for if statement with logical or (||) - with Java's short circuiting, what's the forth condition JaCoCo wants me to cover?

查看:368
本文介绍了用逻辑或(||)测试if语句的覆盖率-由于Java的短路,JaCoCo要我覆盖的第四个条件是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个相当简单的问题,但我很茫然...

This is probably a rather simple question, but I'm at a loss...

我有一个if语句,如下所示:

I have an if statement like the following:

if(TheEnum.A.equals(myEnum) || TheEnum.B.equals(myEnum))

TheEnum可以是ABC,... G(不仅仅是4个选项).

TheEnum can be A, B, C, ... G (more than just 4 options).

JaCoCo(SONAR)告诉我,在这里我可以涵盖四个条件. 那些是什么? 本质上来说,这不是我可以测试的全部

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

我敢肯定我不能专门测试 if(true || true)if(true || 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...?

如果是这样,JaCoCo/Sonar想要我进行测试的第四个选项是什么?

If so, what is the forth option JaCoCo/Sonar wants me to test for?

推荐答案

您是正确的,此代码是短路的.它被编译成大致像这样的字节码(假设Java已经goto了):

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:

因此,在JaCoCo分析字节码时,从其角度来看,您有两个独立的检查:第一个if和第二个if,它们生成四个可能的分支.您可能认为这是一个JaCoCo错误,但我想要对其进行强大的修复并不容易,而且也不是很令人不安,因此您可以忍受它.

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语句的覆盖率-由于Java的短路,JaCoCo要我覆盖的第四个条件是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆