How to make Sonar ignore some classes for codeCoverage metric? – Java

Photo of author
Written By M Ibrahim
azure-java-sdk maven sonarqube

Quick Fix: Configure the sonar.coverage.exclusions property in your SonarQube settings to exclude specific classes from code coverage calculation. This will prevent SonarQube from considering those classes when measuring code coverage metrics.

The Problem:

In a Maven project, you want to exclude certain classes from Sonar’s code coverage metric calculation. You have a Sonar profile with defined exclusions but it’s not working. You also have a correct Cobertura profile but adding it to the Sonar profile does not result in the desired outcome. You need a solution to make Sonar ignore specific classes, like "com/qwerty/dw/publisher/Main.class", for code coverage metric calculation.

The Solutions:

Solution 1: Set `sonar.coverage.exclusions` property

To make Sonar ignore specific classes for the code coverage metric, set the `sonar.coverage.exclusions` property in your Maven profile.
This property allows you to specify the classes that should be excluded from coverage calculation.
The syntax for the property value is a comma-separated list of patterns, where each pattern matches a class or package name.
For example, to exclude all classes in the `foo` package and all classes in the `bar` package, you would set the property value to `foo/**/*,**/bar/*`.
Note that this will only exclude the specified classes from coverage calculation.
All other metrics and issues will still be calculated.

To find the property name for your version of SonarQube, go to the General Settings section of your SonarQube instance and look for the Code Coverage item.
Below the input field, it should give you the property name (typically in the format "Key: sonar.coverage.exclusions").

Solution 2: Use glob anti-patterns to exclude specific files or packages

In your pom.xml file, add the following property to exclude specific files or packages from code coverage analysis:

<properties>
    <sonar.exclusions>**/Name*.java</sonar.exclusions>
</properties>

This property uses glob anti-patterns to exclude any Java class that ends with Name.java. For more information on glob anti-patterns, refer to the SonarQube documentation at Narrowing the Focus.

Solution 3: Exclusion of Code Coverage Calculation for Classes With sonar.coverage.exclusions Property

To disregard certain classes for calculating code coverage in SonarQube, utilize the sonar.coverage.exclusions property. This property accepts a comma-separated list of file path patterns. Here’s an example of its usage in a Maven profile:

<profile>
  <id>sonar</id>
  <properties>
    <sonar.coverage.exclusions>**/config/*,**/model/*</sonar.coverage.exclusions>
  </properties>
</profile>

This configuration excludes all classes residing in directories named ‘config’ and ‘model’ from code coverage calculations.

For excluding a single class explicitly, you can use a path pattern like this:

<sonar.coverage.exclusions>src/main/java/com/some/package/name/GlobalExceptionhandler.java</sonar.coverage.exclusions>

This ensures that the ‘GlobalExceptionhandler.java’ class is excluded without using wildcards.

Solution 4: sonar.coverage.exclusions

When using sonar-scanner for swift for specifying the files to be excluded for code coverage metrics, set sonar.coverage.exclusions in your sonar-project.properties. If you wish to exclude those files from the analysis completely, you can do so using sonar.exclusions. I have successfully employed this method to exclude swift files.

Example
sonar.coverage.exclusions=/*ViewController.swift,/*Cell.swift,**/*View.swift