Why does my sonarqube give 0% coverage while I have unit tests?

Viewed 4538

So I am using SonarQube to improve my code. The problem is that I keep getting 0% coverage. I think my build.gradle file is not correct, but I am not sure that is the problem. Can anyone check for me if my build.gradle is correct or not so that if this is not the problem I keep looking what else is the problem.

My build.gradle:

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "org.sonarqube" version "3.2.0"
    id 'jacoco'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}


dependencies {
    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpclient:4.5'
    implementation('io.jsonwebtoken:jjwt:0.2')
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'junit:junit:4.12'
    implementation 'org.modelmapper:modelmapper:2.4.1'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r'
    /**
     * JUnit jupiter with mockito.
     */
    testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.19.0'

    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
    testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
}

sonarqube{
    properties{
        property 'sonarjava.source', '1.8'
        property 'sonar.java.coveragePlugin', 'jacoco'
        property 'sonar.jacoco.reportPaths', 'build/reports/jacoco/test/jacocoTestReport.xml'
        property 'sonar.java.test.binaries'
    }
}
test {
    useJUnitPlatform()
}

Thanks in advance!

2 Answers

The key is that the SonarQube scanner has to know where the jacoco data file resides. The pervasive annoyance with property-based development is that it's unlikely you will ever see an error message saying you didn't set the correct property. That's what happened in this case.

This depends a bit on what version of SonarQube you're working with, but the property you're setting, "sonar.jacoco.reportPaths", was never intended to point to the Jacoco XML file. The correct property is "sonar.coverage.jacoco.xmlReportPaths".

Add jacocoTestReport task in build.gradle

jacocoTestReport {
reports {
    xml.enabled true
}

}

Run -->

gradle build jacocoTestReport sonarqube

This SonarSource Guide might help you to understand it better https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151

Check build.gradle in this sample project provided in SonarQube documentation -- https://github.com/SonarSource/sonar-scanning-examples/blob/master/sonarqube-scanner-gradle/gradle-basic/build.gradle

Related