Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore code blocks in Sonar code coverage analysis?

In our code, there are quite a lot of logging fragments like this:

if(logger.isDebugEnabled()) {
    logger.debug("...")
}

Is it possible to configure SonarQube so that such code blocks are not included in the code coverage analysis? Writing tests to cover such debug statements does not seem to make much sense...

I found out how to:

  • ignore entire files from coverage analysis
  • ignore issues in code blocks

But I did not find a way of excluding a code block from the coverage analysis only.

like image 832
dokaspar Avatar asked May 18 '15 09:05

dokaspar


1 Answers

Since Sonarqube can import JaCoCo coverage report, you could use an annotation containing "Generated" in its name, as explained here by nineninesevenfour.

In your case, you would replace your log calls with logDebug() calls, and:

    @Generated
    void logDebug(String message) {
        if(logger.isDebugEnabled()) {
            logger.debug("...")
        }
    }
like image 70
VonC Avatar answered Sep 29 '22 19:09

VonC