2

我没有达到 100% 覆盖的代码并且想要。除非我看到 100% 的绿色,否则我想知道我忘记测试什么并去寻找只是为了找出基于该工具的愚蠢的东西,而不是我的测试让我远离它。后来我忘记了,不得不冲洗/重复。

尽管由于异常,所有路径都包含在 testThrow 中,但它不计为运行。

有没有办法重写它,所以它被视为覆盖到难以捉摸的 100% 绿色。

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) 但该行仍然是红色的。

我也试过:

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
4

1 回答 1

1

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

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

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

于 2017-09-17T13:24:19.067 回答