英文:
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
<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>
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
通常情况下,jacoco
和 surefire
插件需要被正确地“集成”,以便使用 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:
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
And the following in surefire:
<argLine>${surefireArgLine}</argLine>
If you really need those additional parameters, just add it to the end of this value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论