代码覆盖率未反映在SonarQube UI中

问题描述

我正在尝试使用SonarQube建立静态代码分析。在SonarQube UI中创建了一个项目。在詹金斯管道中运行声纳扫描仪。

管道成功运行,报告已发布到SonarQube,但未显示预期的代码覆盖范围:

enter image description here

表明已找到测试,但覆盖率仍为0%。

我在pom.xml中使用了jacoco插件

   <pluginManagement>
      <plugins>
          <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.6</version>
        </plugin>
      </plugins>
    </pluginManagement> 

我正在管道中运行jacoco:prepare-agent和jacoco:report。甚至尝试在运行jacoco目标之前运行mvn全新安装,但无济于事。

还要检查在target / surefire-reports下生成的xml文件,该文件显示测试已运行:


测试集:in.javahome.myweb.controller.CalculatorTest

运行测试:2,失败:0,错误:0,跳过:0,经过的时间:0.047秒

SonarQube版本:7.9.4 詹金斯版本:2.259

任何指出问题所在的指针将不胜感激。

谢谢

解决方法

这通常意味着您尚未将jacoco插件与surefire插件正确集成。可以获得代码覆盖率结果的唯一方法是,如果surefire以生成代码覆盖率数据的方式运行。默认情况下,它们不会集成在一起。

这两个插件的重要组成部分如下:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.maven.plugin.version}</version>
            <executions>
                <!-- Prepares the property pointing to the JaCoCo runtime agent which 
                    is passed as VM argument when Maven the Surefire plugin is executed. -->
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>

这:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- Sets the VM argument line used when unit tests are run. -->
                <argLine>${surefireArgLine}</argLine>