I'm currently trying to configure a Sonarcloud in my SpringBoot project with gradle and bitbucket-pipelines. But for every PR and every branch it always shows 0% line coverage.
I've added the plugin configuration to my build.gradle
file:
id "org.sonarqube" version "3.4.0.2513"
and this is my bitbucket-pipelines.yml
file
image: openjdk:11
clone:
depth: full # SonarCloud scanner needs the full history to assign issues properly
definitions:
caches:
sonar: ~/.sonar/cache # Caching SonarCloud artifacts will speed up your build
steps:
- step: &build-test-sonarcloud
name: Build, test and analyze on SonarCloud
caches:
- gradle
- sonar
script:
- ./gradlew build sonarqube
artifacts:
- build/libs/**
pipelines: # More info here: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html
branches:
master:
- step: *build-test-sonarcloud
pull-requests:
'**':
- step: *build-test-sonarcloud
Everything seems correctly configured on Bitbucket, the pipelines run for every PR and commit but all of them fail due the 0% coverage. I was expecting the correct test coverage to be shown on Sonar and Bitbucket PR decoration. Is there any configuration missing?
CodePudding user response:
While SonarQube supports the reporting of test coverage, by itself it doesn't generate it. You have to generate it by using a third-party tool and then configure SonarQube to take the result of that third-party tool into account.
From their docs:
SonarQube supports the reporting of test coverage as part of the analysis of your Java project. However, SonarQube does not generate the coverage report itself. Instead, you must set up a 3rd party tool to produce the report as part of your build process.
Among the 3rd party tools, SonarQube directly supports JaCoCo, so since you're using gradle, you just need to apply the plugin in your build.gradle
:
plugins {
id "jacoco" // <-- add here
id "org.sonarqube" version "3.4.0.2513"
}
Then configure the JaCoCo task:
jacocoTestReport {
reports {
xml.enabled true
}
}
According to SonarQube documentation, they detect the default location where JaCoCo stores the coverage report automatically, so no further configuration should be required.
Mind you, you can also use other coverage tools, you're not limited to JaCoCo alone, but it should be the simplest to implement.
Documentation on Java test coverage can be found here: https://docs.sonarqube.org/latest/analysis/test-coverage/java-test-coverage/