EclEmma fails to count method that throws an exception as covered

你说的曾经没有我的故事 提交于 2019-12-11 00:25:37

问题


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.

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

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();
    }
}

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

I also tried:

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

回答1:


You can find the same thing in EclEmma documentation:

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.



来源:https://stackoverflow.com/questions/33265287/eclemma-fails-to-count-method-that-throws-an-exception-as-covered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!