【问题标题】:Kotlin, Java 8 and Sonar coverage showing as 0Kotlin、Java 8 和声纳覆盖率显示为 0
【发布时间】:2021-01-25 20:32:40
【问题描述】:

我有一个使用 Java 8 的 kotlin 项目,我使用 Sonar 来测量代码覆盖率 我正在使用最新版本的 Sonar,并注意到由于 openjdk 的变化,覆盖率有所下降。我的测试使用 Mockito 和 PowerMockito。一些测试由于反射错误而失败。

当我添加以下 jvm args - 错误消失 - --add-opens java.base/jdk.internal.loader=ALL-UNNAMED

但是,如果我将 maven-surefire-plugin 添加到我的 pom.xml - 代码覆盖率在声纳中显示为 0

我使用 Jacoco 来衡量覆盖率 - 这是我的 pom.xml 的相关部分

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--https://stackoverflow.com/questions/39750348/how-to-add-vm-args-using-pom-xml-plugin/39751176 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <argLine>--add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
                </configuration>
            </plugin>

因此,如果我不包含 surefire 插件,我的声纳代码覆盖率会显示出来,但由于上述错误,一些测试会失败。 如果我包含了 surefire 插件,我的所有测试都会通过,但我的代码覆盖率在声纳中显示为 0

我正在使用以下命令运行我的测试并使用 Sonar 进行分析

mvn clean install -DskipTests=false -Dmaven.test.failure.ignore=true sonar:sonar

无论如何,surefire 插件和 jacoco 插件是否可以共存以查看声纳的覆盖范围,或者人们可以提供任何进一步的建议吗?

【问题讨论】:

    标签: java maven kotlin sonarqube sonarqube-scan


    【解决方案1】:

    通常,必须正确“集成” jacoco 和 surefire 插件,以便使用 jacoco 代理运行单元测试。如果你没有指定一个万能插件,我很惊讶你会得到覆盖。我想一定有默认值可以做到这一点。在我看来,最好是明确的,所以很明显它们是集成的。

    您的 jacoco 插件中应该有以下内容:

                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
    

    以下是万无一失的:

                    <argLine>${surefireArgLine}</argLine>
    

    如果你真的需要那些额外的参数,只需将其添加到该值的末尾即可。

    【讨论】:

    • 成功了@David M. Karr。我还需要包含一个额外的 junit 瞻博网络依赖项,但现在一切都很好并且可以正常工作。非常感谢您对此提供的帮助
    猜你喜欢
    • 2012-07-18
    • 2013-11-21
    • 2015-03-21
    • 1970-01-01
    • 2015-05-15
    • 2012-09-30
    • 2015-10-06
    • 2017-05-31
    • 2018-09-22
    相关资源
    最近更新 更多