java - Jacoco 输掉了测试。覆盖率始终为 0

标签 java maven jacoco jacoco-maven-plugin

Jacoco 插件丢失了 JUnit 测试,报告总是 0% 但测试存在。我认为问题出在我的 pom.xml 上。帮我找出错误。

Pom.xml on GitHub

也许它与checkstyle 插件有冲突?

部分 Jacoco 设置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.9.1</version>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.7.0.1746</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <reportFormat>plain</reportFormat>
                <includes>
                    <include>**/*Test*.java</include>
                    <include>**/*IT*.java</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>PACKAGE</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.0</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>3.1.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>${checkstyle.version}</version>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>checkstyle</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

最佳答案

没有执行测试,所以没有覆盖。


build log你有

[INFO] Tests are skipped.

这可能是由于您的 .github/workflows/build.yml 中的 -DskipTests

run: mvn -B clean package -DskipTests

执行mvn test产生

[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ bridge ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

因为你在依赖项中有 junit-jupiter, 但是没有junit-vintage-engine(见mvn dependency:tree的输出), 而您的测试是使用 JUnit 4 编写的。


在以下删除 junit-vintage-engine 排除之后

             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
+            <!--
             <exclusions>
                 <exclusion>
                     <groupId>org.junit.vintage</groupId>
                     <artifactId>junit-vintage-engine</artifactId>
                 </exclusion>
             </exclusions>
+            -->

执行mvn test产生

[INFO] Tests run: 47, Failures: 0, Errors: 0, Skipped: 0

和下面的报告

report

关于java - Jacoco 输掉了测试。覆盖率始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64590837/

相关文章:

java - 是否可以在控制台中重写上一行?

java - 为什么我的虚拟机中的 R 计算不一致?

grails - Gant构建脚本,如何检索我要执行的任务

java - jacoco 测试报告不排除包

java - WaitforElement 超时错误?

java - 无法添加窗口——对此窗口类型的权限被拒绝

Java NIO select() 返回时没有选择键 - 为什么?

java - Ivy、Maven - 映射传递依赖

java - 如何配置 Maven cargo 下载部署 Wildfly 10 并添加测试用户?

gradle - 如何使用Gradle从JaCoCo测试报告中排除隐式Groovy闭包类