How to ignore Logger related If conditions while using Cobertura?

1.9k Views Asked by At

I am trying to increase JUnit test case coverage using Cobertura.

The code has quite a lot of Loggers, and also If conditions to check whether Info or Debug is enabled. Ex:

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

Now obviously, there will be no Else part for this. The above code will have a 50% branch coverage. How do I make that 100%?

I have tried ignoring Logger calls in instrumentation:

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

But that just brought down the coverage to 0%.

Is there a workaround for this?

Any help would be appreciated.

2

There are 2 best solutions below

2
On BEST ANSWER

It looks like you are using SLF4J - so the simplest way would be removing those guard clauses as you can use parameterized logging.

IntelliJ IDEA will convert to this for you - Menu>Analyze>Run Inspection By Name == "Non-Constant String concatenation as argument to logging call" ) and offers a quick fix for those it finds. See http://www.slf4j.org/faq.html#logging_performance

1
On

Your config

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

fails. Use

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

and remove the exclude config.