我在 java 1.7 上使用 cobertura 2.6 和 maven
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
但如果我使用 java7 的新 try-with-resource 特性,它会告诉我“不存在的 catch” block 在测试中丢失...它标记了 try-block 的右括号
有什么问题吗?或者我如何测试它们?
最佳答案
问题是您可能没有用资源 block 测试所有情况。 任何时候你写这样的东西:
try(Autocloseable ac = new Autocloseable()) {
//do something
} catch(Exception e) {
//Do something with e
}
编译器解释如下:
Autocloseable ac = null;
Exception e = null;
try {
ac = new Autocloseable();
//Do something
} catch (Exception e1) {
e = e1
//Do something with exception
} finally {
if(ac != null) {
try {
ac.close();
} catch (Exception e2) {
throw e == null? e2 : e;
}
if(e != null ) throw e;
}
}
它并不完全像那样,但它是整体思路,所以你会看到实际的代码分支比你想象的要多得多。 我希望这能让您了解如何提高覆盖率。
关于java - Cobertura Java7 尝试使用资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21727663/