Jacoco 覆盖率专家

问题描述 投票:0回答:2

运行 mvn test 时,我的 Spring Boot 应用程序构建成功。当我执行 mvn clean install 或 mvn clean verify 时,构建失败。这是因为我将最小代码覆盖率配置为 80%。当我运行 mvn clean install/verify 时,结果显示指令覆盖率为 0.00,但预期最小值为 0.50。

在设置最小阈值之前,运行 mvn clean install 给出以下结果,即覆盖 65%。我的配置中缺少什么?我错过了什么吗?我怎样才能让 jacoco 选择正确的代码?

这是我的pom

    <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <version>0.8.5</version>
            <classifier>runtime</classifier>
            <scope>test</scope>
        </dependency>
        <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.3</version>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.1</version>
                <dependencies>
                    <dependency>
                        <groupId>com.github.bitmc</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>8.35.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </pluginManagement>

    <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <excludes>
            <exclude>*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <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>
                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>PACKAGE</element>
                                <excludes>
                                    <exclude>com.pip</exclude>
                                </excludes>
                                <limits>
                                    <limit>
                                        <counter>INSTRUCTION</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.50</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我正在使用java版本“1.8.0_202”

java spring spring-boot maven jacoco
2个回答
0
投票

如果您将 Jacoco 与 PowerMockito 一起使用,并且您想要覆盖的类是在 PrepareForTest 中获取的,那么在这种情况下,它将显示 0% 的覆盖率。 尝试从PrepareForTest中删除目标类


0
投票

你的插件配置需要这样设置:

        ...
         <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <configuration>
                <excludes>
                    <exclude>**/PACKEGE YOU WANT EXCLUDE.*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>

                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>BUNDLE</element>
                                <limits>
                                    <limit>
                                        <counter>INSTRUCTION</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>65%</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>

                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

在您的示例中,标签插件不在插件标签中。

© www.soinside.com 2019 - 2024. All rights reserved.