EclEmma无法计算抛出所涵盖异常的方法 [英] EclEmma fails to count method that throws an exception as covered

查看:100
本文介绍了EclEmma无法计算抛出所涵盖异常的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并没有达到100%的代码覆盖范围,并且想要这么做。除非我看到100%的绿色,否则我想知道我忘记测试什么了,而去寻找却只是基于该工具发现了一些愚蠢的东西,而不是我的测试使我无法进行测试。然后,后来我忘记了,必须冲洗/重复。

I am not getting to the 100% code covered and would like to. Unless I see the 100% green I wonder what I forget to test and go hunting only to find out silly things based on the tool and not my test are keeping me from it. Then later I forget and have to rinse/repeat.

尽管所有路径都被testThrow覆盖了,但由于例外,它不被视为运行。

While all paths are covered in testThrow because of the exception it is not counted as run.

有没有办法重写它,因此它被认为覆盖着那难以捉摸的100%绿色。

Is there a way to re-write it so it is seen as covered towards that elusive 100% green.

public class Dummy {
    public void testThrow() throws Exception {
        throwException();       // This line is red and is seen as not covered.
    }

    private void throwException() throws Exception {
        throw new Exception();
    }
}

public class DummyTest() {
    @Test
    public void testThrow() throws Exception {
        new Dummy().testThrow();
    }
}

我添加了@Test(expected = Exception.class)但该行仍为红色。

I added @Test(expected=Exception.class) but the line is still red.

我也尝试过:

public void testThrow() throws Exception {
    try {
        throwException();       // This line is STILL red
    }
    catch(Exception e) {
        throw e;                // This line becomes green (as expected)
    }
}                               // This line is now also red


推荐答案

您可以在中找到相同的内容EclEmma文档


为什么带有预期异常的JUnit4测试用例显示为未涵盖?



具有预期异常的JUnit4测试用例即使已执行,也显示为未涵盖
。原因是底层的
JaCoCo代码覆盖库仅在执行
某些探测时才将代码视为已执行。对于标有
@Test {expected = ...}的成功测试用例,情况并非如此。

Why are JUnit4 test cases with expected exceptions shown as not covered?

JUnit4 test cases with expected exceptions are shown as not covered even though they were executed. The reason for this is that underlying JaCoCo code coverage library only considers code as executed when certain probes are executed. For successful test cases marked with @Test{expected=...} this is not the case.

这篇关于EclEmma无法计算抛出所涵盖异常的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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