AniketGole
AniketGole

Reputation: 1295

sonarqube+jacoco+junit code coverage showing 0% in sonarqube dashboard

I am using sonarqube + jacoco for codecoverage, i have junit test case for my java code in below dir structure.

src/main/java/code/abc.java
src/main/test/code/Testabc.java

I want a code coverage on sonarqube dashboard and i am using gradle for the same.

apply plugin: "java"
apply plugin: "org.sonarqube"
apply plugin: "jacoco"

jacoco {
toolVersion = "0.7.9"
reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")
}

sonarqube {
    properties {
        property "sonar.sources", "."
        property "sonar.projectName", "javacode"
        property "sonar.projectKey", "javacode"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.java.binaries", "/data/tmp/env/"
        property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
    }
}

buildscript {

   repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"

  }
}

I am running gradle sonarqube

I dont know where i am doing wrong? if any one can help to get the coverage of test case written from java code.

Upvotes: 1

Views: 1002

Answers (2)

Paulo Merson
Paulo Merson

Reputation: 14515

Here's the minimal config that is working with

  • gradle 7.5
  • sonarqube gradle plugin 3.3
  • jacoco latest version

Gradle snippet:

apply plugin: "org.sonarqube"
apply plugin: "jacoco"

// jacoco task config not needed since I'm using the defaults (latest version, default dir)
// jacoco {
//     toolVersion = "0.7.9"
//     reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")
// }

sonarqube {
    properties {
        property "sonar.sources", "."
        property "sonar.projectName", "javacode"
        property "sonar.projectKey", "javacode"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.java.binaries", "/data/tmp/env/"
//      no need to define sonar.jacoco.reportPaths or sonar.coverage.jacoco.xmlReportPaths as sonarqube 
//      finds the xml report in the default location (build/reports/jacoco/test/jacocoTestReport.xml)
//      property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
    }
}

jacocoTestReport {
    reports {
        xml.enabled true   // to be sure xml report is created (sonarqube knows jacoco's default report location)
    }
}

Upvotes: 0

Stanislav
Stanislav

Reputation: 28136

It seems to me, that Jacoco test reports are generated in one directory, but SoanrQube is configured to look into another one. Here is the directory you save reports:

reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")

and here is Sonar configuration to look up Jacoco results:

property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"

There is customJacocoReportDir directory missed

Upvotes: 0

Related Questions