I am using sonar for code analysis in my current project and my question is how can I specify certain methods, particularly getters and setters to be excluded when counting code coverage.
Also I realized that for the @Component and @Value annotations the line coverage is 0, even when there is a unit test which uses the specific bean. Interestingly, the @Autowired is skipped as it should be.
Where can I change this configuration/add some exceptions?
To scan a specific codebase you run the SonarQube scanner. This is a local process that analyses your code then sends reports to the SonarQube server. The code coverage metric has to be computed outside of SonarQube using a different tool. The result is then ingested into SonarQube and shown within its web interface.
Test coverage reports and test execution reports are important code quality metrics that you can import into SonarQube. Test coverage reports tell you the percentage of your code that is covered by your test cases.
Sometimes, an analyzed project may contain some generated code that we want to exclude and narrow the focus of SonarQube checks. Let's exclude our class by defining sonar.exclusions in our pom.xml: In that case, we've excluded a single file by its name. Checks will be performed for all files except that one. We can also use file matching patterns.
Test execution reports tell you which tests have been run and their results. SonarQube doesn't run your tests or generate reports. To include coverage results in your analysis, you need to set up a third-party coverage tool to generate reports and configure SonarQube to import those reports.
There's currently no way to do exclude specific methods from code coverage.
For your second case, the problem is not on Sonar side but on the coverage tool that you use (Cobertura by default, but you may have changed it to Jacoco, Emma or Clover). You can try all those different tools to see which one gives you the best results. FYI, Jacoco will soon be the default coverage tool for Sonar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With