1

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.

AniketGole
  • 889
  • 2
  • 11
  • 22
  • Did you read any documentation? See [here](https://docs.sonarqube.org/latest/analysis/coverage/) and [here](https://community.sonarsource.com/t/will-sonar-jacoco-reportpaths-work-in-sonar-7-lts/9598). Update your question adding version info and log output. – Jeroen Heier Aug 19 '19 at 05:29

2 Answers2

0

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

Stanislav
  • 27,441
  • 9
  • 87
  • 82
0

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)
    }
}
Paulo Merson
  • 13,270
  • 8
  • 79
  • 72