我没有达到 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