【问题标题】:Jacoco lost test. Coverage always 0Jacoco 测试失败。覆盖率始终为 0
【发布时间】:2021-02-11 21:07:57
【问题描述】:

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>

【问题讨论】:

    标签: java maven jacoco jacoco-maven-plugin


    【解决方案1】:

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


    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
    

    以及下面的报告

    【讨论】:

    • 本地测试得到相同的结果。我被困在本地测试配置步骤上。我删除了-DskipTests,但没有任何改变。
    猜你喜欢
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2021-05-09
    • 2012-09-07
    • 2016-04-07
    • 1970-01-01
    • 2016-11-11
    • 2023-03-14
    相关资源
    最近更新 更多