Integrating unit and integration tests with SonarQube



I am trying to report unit and integration tests coverage to SonarQube from my Java project to see the metrics for both separately on the SonarQube dashboard.

Tried so far:
I have added the maven jacoco plugin to the pom.xml in my project and configured it to generate 2 exes, jacoco-ut.exec and jacoco-it.exec.
I am trying to deal with the unit tests first since the integration tests are failing in the build for some other reason. Here is the command I use to run the unit tests in my Jenkinsfile:

stages {
    stage('Build') {
      agent { label "build-centos" }
      environment {
        branch_name = "${env.BRANCH_NAME}"
      steps {
          writeFile file: '.build-docker', text: ''
          script {
            initPipeline majorVersion: '4',
              serviceName: 'forensics-service'
            parallel (
              app: {
                    sh """
                    mvn -DnewVersion=$VERSION versions:set
                    mvn -X -Dproject.docker.project=$DOCKER_REPO*** -Dsonar.login=*** -
                    Dsonar.password=***${branch_name} clean verify sonar:sonar

                    mvn -Dproject.docker.project=$DOCKER_REPO -Ddocker.allContainers docker:stop -P 

                    mvn -Dproject.docker.project=$DOCKER_REPO verify -P integration-test

                    mvn -Dproject.docker.project=$DOCKER_REPO -DskipUTs=true install docker:push 

                    mvn -X -Dproject.docker.project=$DOCKER_REPO*** -Dsonar.login=*** - 
                    Dsonar.password=***${branch_name} - 
                    Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-it - 

              tests: {
                sh """
                 mvn install -f tests/pom.xml
                buildDocker directory: 'tests', image: "$NAME-tests", arguments: "--build-arg version=$VERSION --build-arg branch=${env.BRANCH_NAME} ."
              helm: {
                  '--app-version', env.VERSION,
                  '--set', "cloud15-helm.k8s-config-hook.service=$NAME",
                  '--set', "cloud15-helm.k8s-config-hook.serviceVersion=$VERSION",
                  '--set', "cloud15-helm.k8s-config-hook.oauthClient=false")
          stage("Quality Gate"){
              steps {
                  timeout(time: 5, unit: 'MINUTES') {
                      // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
                      // true = set pipeline to UNSTABLE, false = don't
                      waitForQualityGate abortPipeline: true
      post {
        always {
          sh """
            mvn -Dproject.docker.project=$DOCKER_REPO  -Ddocker.allContainers docker:stop -P integration-test
          junit '**/target/surefire-reports/*.xml'

            $class           : 'JacocoPublisher',
            execPattern      : '**/target/coverage-reports/*.exec',
            classPattern     : '**/target/classes/com/proofpoint/forensicsstore',
            sourcePattern    : 'src/main/java',
            exclusionPattern : '**/*Test.class'

Right now my blocker is, sonar qube being able to read the unit tests count and most of the other metrics except code coverage, which is important to me.

Thanks in advance.


Welcome to the community!

It looks like a few hours after posting this, you contributed on the same topic to another thread and got answered there. Unless you say something different, I’ll consider this topic answered.


Yes this was answered by a support ticket. Thank you!