Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up properly SonarQube's for Code Coverage

I'm using JUnit5 on a SpringBoot backend application server using Maven. Here is the sonar-project.properties file that is at the root of the project:

sonar.host.url=https://sonarcloud.io
sonar.login=xxx
sonar.organization=xxx
sonar.projectKey=xxx

sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.java.source=12

sonar.sources=src/main/java
sonar.test=src/test
sonar.java.binaries=target/classes
sonar.junit.reportPaths=target/test-results/TEST-**.xml

I use the sonar-scanner command line to run update the project after a build/test.

The Overview board on sonar-cloud looks like this:

overview

I at least got the unit tests to be recognized, but somehow I'm still at 0% in terms of code coverage. Furthermore, here is the Measures board:

measures

Apparently, my tests do not cover any lines whatsoever. Now, I'm aware that this means that I most probably didn't hook up the test-results properly, but I'm not sure how to do that.

What puzzles me, too, is that despite SonarQube recognizing my tests, it actually says that the lines-of-code of the tests themselves aren't tested. What is this supposed to mean?

Testing tests?

like image 838
payne Avatar asked Nov 23 '19 19:11

payne


People also ask

How do I improve sonar code coverage?

to increase your code coverage, the suggestion is to write tests. It can be unit tests (easiest way) or other tests (Integration test, System tests) which may contribute to coverage when a tool can report coverage for these.

Does SonarQube give code coverage?

SonarQube is a tool which aims to improve the quality of your code using static analysis techniques to report: code coverage.


1 Answers

From SonarQube's documentation:

SonarSource analyzers do not run your tests or generate reports. They only import pre-generated reports.

A popular library for generating code coverage for Java is Jacoco.

SonarQube provides this guide to create and import Jacoco's reports.

like image 62
Michele Dorigatti Avatar answered Oct 02 '22 19:10

Michele Dorigatti