1

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.

ybbs
  • 49
  • 1
  • 8

2 Answers2

1

Your config

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

fails. Use

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

and remove the exclude config.

wero
  • 32,544
  • 3
  • 59
  • 84
1

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

  • I use STS(Spring Tool Suite) for code development. And it is a requirement to have these clauses to check if **Logger Info** is enabled. – ybbs Feb 18 '16 at 11:13
  • 1) Why is it a problem that you are using STS/Eclipse ? IntelliJ community edition is a free install and use to automatically convert/fix your code across the project (saving a lot of time), why not install it then run the conversion and revert to using eclipse? 2) Did you read that link about logging performance at http://www.slf4j.org/faq.html#logging_performance ? As you are using SLF4J there is no need for guard clauses. Maybe you should push back on that requirement with whomever mandated it. – user3128956 May 01 '18 at 09:34