15

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.

dokaspar
  • 8,186
  • 14
  • 70
  • 98

3 Answers3

3

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("...")
        }
    }
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

for files:

sonar-project.properties

sonar.exclusions=**/someglobalfolder*/** , **/someotherglobalfolder/*.js

For commenting out blocks:

See article: Turning Sonar off for certain code

Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63
-2

I have also faced same problem.Rather ignoring it I used following two techniques

1)Mock These Logger Using any Mocking Framework example Mockito,Powermockito,PowerMock etc.Use same Mocking Code Across Test Classes wherever Applicable

2) Keep logback-test.xml(or logging config file for whatever logging framework you using) in class path and set Lower Log Level Like Trace.So let test classes to load logger to print these statements.

This will help in showing details about how test cases executing statement statement

Nazik
  • 8,696
  • 27
  • 77
  • 123
Harsimranjit Singh Kler
  • 2,006
  • 1
  • 18
  • 21