如何在使用 Cobertura 时忽略与 Logger 相关的 If 条件?

How to ignore Logger related If conditions while using Cobertura?

我正在尝试使用 Cobertura 增加 JUnit 测试用例的覆盖率。

代码中Logger比较多,还有If条件判断是否开启了Info或Debug。例如:

if (LOGGER.isInfoEnabled()) {
    LOGGER.info("Some info...");
}

很明显,这里没有 Else 部分。上面的代码将有 50% 的分支覆盖率。我如何做到 100%?

我试过在检测中忽略 Logger 调用:

<instrumentation>
    <ignores>
        <ignore>org.slf4j.Logger.*</ignore>
    </ignores>
    <excludes>
        <exclude>**/Example.class</exclude>
    </excludes>
</instrumentation>

但这只是将覆盖率降至 0%。

有解决办法吗?

如有任何帮助,我们将不胜感激。

您的配置

<ignore>org.slf4j.Logger.*</ignore>

失败。 使用

<ignore>org.slf4j.*</ignore>

并删除排除配置。

看起来您正在使用 SLF4J - 因此最简单的方法是删除那些保护子句,因为您可以使用参数化日志记录。

IntelliJ IDEA 会为您转换为这个 - Menu>Analyze>运行 Inspection By Name == "Non-Constant String concatenation as argument to logging call" )并为它找到的那些提供快速修复。 参见 http://www.slf4j.org/faq.html#logging_performance