'Exclude class from sonar qube coverage with attribute java

There is way to exclude class from coverage by attribute in java?

In C# I can use [ExcludeFromCodeCoverage].

In java I am trying:

@Retention(RetentionPolicy.CLASS)
public @interface ExcludeFromCodeCoverage  {
}

and use @ExcludeFromCodeCoverage for class which I want exclude. But it don't work.

I saw solution to add properties to pom.xml:

<properties>
    <sonar.coverage.exclusions>foo/**/*,**/bar/* 
    </sonar.coverage.exclusions>
</properties>

but for me it don't look good. I have some files which are models from my database. And I don't need test it.



Solution 1:[1]

If you have administrator access to the SonarQube dashboard (or if you can ask an administrator to do it for you), you can add a pattern which, when present in a file, will cause that file to be excluded. Note that this will exclude the whole file, not just a class; therefore, to exclude a class, make sure it's in its own file with no other classes.

In the SonarQube dashboard, go to Project Settings > General Settings > Analysis Scope > Issues and next to "Ignore issues in files" add the regular expression pattern "@ExcludeFromCodeCoverage".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 k314159