EclEmma(Jacocco)显示即使执行了该行也未覆盖 [英] EclEmma(Jacocco) shows line not covered even if the line is executed

查看:93
本文介绍了EclEmma(Jacocco)显示即使执行了该行也未覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高代码的行覆盖率.在我调用某个方法的地方执行了一行代码,而该方法又将引发异常.但是行覆盖率显示为红色,表示未执行该行.

I am trying improve line coverage for my code. A line gets executed where i am calling a method which in turn will throw an exception. But line coverage shows red color meaning the line was not executed.

代码:

1 public void myMethod(SomeService someService){
2   throwException(someService);
3 }

4 public void throwException(SomeService someService){
5   THROW an exception here
6 }

在测试类中,我正在准备SomeService所需的数据,并从上述代码中调用myMethod.第2行显示红色,即使该行已执行并按预期方式引发异常.请建议如何显示行覆盖范围以增加行覆盖范围.

In my test class I am preparing SomeService required data and calling myMethod from above code. line # 2 shows red color even though the line gets executed and throws exception as expected. Please suggest how can i show the line covered to increase the line coverage.

推荐答案

请在发布问题之前使用搜索,因为这广泛地 EclEmma常见问题解答

Please use search before posting questions, because this is widely known JaCoCo limitation described both in EclEmma FAQ and in JaCoCo FAQ:

带有例外的源代码行未显示任何内容.为什么?

JaCoCo使用所谓的探测器确定代码执行.将探针插入控制流中的某些位置.当执行后续探测时,代码被视为已执行.如果发生例外情况,则会在中间的某个位置中止此类指令序列,并且相应的源代码行也不会标记为已覆盖.

Source code lines with exceptions show no coverage. Why?

JaCoCo determines code execution with so called probes. Probes are inserted into the control flow at certain positions. Code is considered as executed when a subsequent probe has been executed. In case of exceptions such a sequence of instructions is aborted somewhere in the middle and the corresponding line of source code is not marked as covered.

换句话说:如果方法调用总是像您的示例一样引发异常,则此方法调用的行将永远不会被标记为完全覆盖的绿色.看到与方法调用一致的唯一方法是,在此方法调用不会引发异常的情况下进行执行/测试.

In other words: if method call always throws exception like in your example, then line of this method call will never be marked as fully covered aka green. The only way to see line with method call as covered - is to have an execution/test, when this method call does not throw exception.

这篇关于EclEmma(Jacocco)显示即使执行了该行也未覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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