我有一个方法正在尝试进行单元测试。我无法发布实际代码,但它看起来像这样:
public int getTotal() throws MyException {
int total = 0;
try (ExternalResource externalResource = ExternalService.getResource()) {
try (OtherExternal otherResource = externalResource.getOtherResource()) {
if (someCondition) {
total = otherResource.getTotal();
}
}
}
}
JaCoCo 告诉我每个 try-with-resource block 上都缺少 4/8 个分支。我正在测试 someCondition 为 true 和 someCondition 为 false,JaCoCo 显示该 block 已完全覆盖。
我读了this question ,我从接受的答案中了解到问题在于字节码的生成方式。
我希望能够更好地理解如何识别生成的各种分支,然后我可以更好地判断是否测试它们(它们是否无法访问等)。
最佳答案
根据 change history在版本 0.8.2 中:
Branches and instructions generated by javac 11 for try-with-resources statement are filtered out
我已经使用 openjdk java8 在本地对此进行了测试,我的 try-with-resources 现在报告了 100% 的分支覆盖率(即使在我的测试中从未抛出 IOException
)。
虽然测试这种行为是件好事,但有时您无法轻易重现此类异常。例如,在一个只返回一个开放端口的方法中:
public int getOpenPort() {
try (ServerSocket boundSocket = new ServerSocket(0)) {
return boundSocket.getLocalPort();
}
}
我知道没有什么简单的方法可以强制此代码抛出 IOException
而无需添加一堆令人困惑和不必要的复杂代码,只是为了通过分支覆盖检查。幸运的是,新的 (v0.8.2) jacoco 库通过调用 Assert.assertNotEquals(0, portChecker.getOpenPort());
的单个测试为该方法提供了 100% 的覆盖率。
关于java - JaCoCo 分支覆盖尝试资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334555/