java - EclEmma(Jacoco) 显示未覆盖的行,即使该行已执行

标签 java unit-testing junit mockito jacoco

我正在尝试改进代码的行覆盖率。在我调用一个方法的地方执行了一行,该方法又会抛出异常。但是行覆盖显示红色,表示该行未执行。

代码:

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 行显示红色,即使该行被执行并按预期抛出异常。请建议我如何显示覆盖的线路以增加线路覆盖率。

最佳答案

Please use search在发布问题之前,因为这是 widely known JaCoCo 限制在 EclEmma FAQ 中都有描述在JaCoCo FAQ :

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.

换句话说:如果方法调用总是像您的示例中那样抛出异常,那么此方法调用的行将永远不会被标记为完全覆盖,也就是绿色。看到方法调用是否涵盖的唯一方法是执行/测试,当此方法调用不抛出异常时。

关于java - EclEmma(Jacoco) 显示未覆盖的行,即使该行已执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032393/

相关文章:

java - 格式错误的 URL 没有协议(protocol)错误

java - 如何持久保存 Map<String, List<String>>?

C# 单元测试 - 奇怪的项目目录

Eclipse JUnit runner 无法在双击时找到 ScalaTest 方法

java - 当返回类型为项目的PaginatedList时,如何模拟mapper.query方法

java - 有没有办法创建每个条目具有不同过期时间的缓存

java - 来自工作线程时,Android Webview loadUrl 不起作用

c# - 单元测试断言的最佳实践

ruby - 如何测试 Sidekiq 作业失败?

java - PowerMockRule 与 EasyMock 一起 NOT Moquito