Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonarqube: view unit tests that cover the source

We have a CI setup in Bamboo which runs Junit Tests and computes Unit Test Coverage using Jacoco. Then we run Sonar plugin for source code analysis. Everything is working great and we can see the analysis on the SonarCube server, inlcuding coverage, but we would like to see exactly which tests cover certain line of code. Right now it just says: covered by unit tests.

Is there a way to do that?

like image 232
jny Avatar asked Jun 06 '16 16:06

jny


1 Answers

I found an answer in the sample sonar project: https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/ut/ut-maven-jacoco.

Jacoco listener has to be configured for surefire plugin.

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- Minimal supported version is 2.4 -->
        <version>2.13</version>
        <configuration>
          <properties>
            <property>
              <name>listener</name>
              <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
          </properties>
        </configuration>
      </plugin>

and dependency added:

   <dependency>
      <groupId>org.sonarsource.java</groupId>
      <artifactId>sonar-jacoco-listeners</artifactId>
      <version>3.8</version>
      <scope>test</scope>
    </dependency>

Also some combination of the following parameters for sonar plugin was needed:

sonar.java.surefire.reportspath 
sonar.junit.reportsPath
sonar.tests=src/test

The last one sealed the deal

like image 165
jny Avatar answered Oct 11 '22 01:10

jny