Sonarqube: view unit tests that cover the source
Asked Answered
R

1

7

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?

Rotow answered 6/6, 2016 at 16:37 Comment(0)
R
6

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

Rotow answered 6/6, 2016 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.