Kotlin,Java 8和Sonar覆盖率显示为0。

huangapple go评论57阅读模式
英文:

Kotlin, Java 8 and Sonar coverage showing as 0

问题

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

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

然而,如果我在我的pom.xml中添加maven-surefire-plugin - 代码覆盖率在Sonar中显示为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插件,我的Sonar代码覆盖率会显示,但由于上述错误,一些测试会失败。
如果我包括surefire插件,所有的测试都会通过,但我的Sonar代码覆盖率显示为0。

我正在使用以下命令运行我的测试并与Sonar一起分析:

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

是否有任何方式让surefire插件和jacoco插件共存,以便在Sonar中查看覆盖率,或者有人能够提供进一步的建议?

英文:

I have a kotlin project using Java 8 and I use Sonar to measure code coverage
I am using the latest version of Sonar and noticed the coverage has gone down due to changes in the openjdk. My tests are using Mockito and PowerMockito. Some tests are failing due to reflection errors.

When I add the following jvm args - the errors go away - --add-opens java.base/jdk.internal.loader=ALL-UNNAMED

However, if I add the maven-surefire-plugin to my pom.xml - the code coverage shows as 0 in Sonar

I use Jacoco to measure the coverage - here is the relevant section of my pom.xml

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

So if I dont include the surefire plugin, my sonar code coverage shows up but some tests fail due to the above mentioned error.
If I include the surefire plugin, all my tests pass but my code coverage shows as 0 in sonar

I am using the following command to run my tests and analyze with Sonar

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

Is there anyway the surefire plugin and jacoco plugin can co exist so as to see coverage in sonar or can folks provide any further recommendations?

答案1

得分: 2

通常情况下,jacocosurefire 插件需要被正确地“集成”,以便使用 jacoco 代理运行单元测试。如果您没有指定 surefire 插件,但却获得了覆盖率,我感到很惊讶。我想必定存在默认设置使此成为可能。在我看来,最好是明确指出,以确保它们被整合在一起。

您可能需要在 jacoco 插件中包含以下内容:

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

并且在 surefire 中添加以下内容:

<argLine>${surefireArgLine}</argLine>

如果您真的需要这些额外的参数,只需将其添加到该值的末尾。

英文:

Typically, the jacoco and surefire plugins have to be "integrated" properly, so that unit tests are run with the jacoco agent. I'm surprised that you get coverage if you don't specify a surefire plugin. I imagine there must be defaults that make that happen. In my view, it's best to be explicit, so it's clear they are integrated.

You should probably have the following in your jacoco plugin:

				&lt;execution&gt;
					&lt;id&gt;pre-unit-test&lt;/id&gt;
					&lt;goals&gt;
						&lt;goal&gt;prepare-agent&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;configuration&gt;
						&lt;propertyName&gt;surefireArgLine&lt;/propertyName&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;

And the following in surefire:

				&lt;argLine&gt;${surefireArgLine}&lt;/argLine&gt;

If you really need those additional parameters, just add it to the end of this value.

huangapple
  • 本文由 发表于 2020年10月11日 17:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定