jacoco覆盖率maven

vwhgwdsa  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(371)

当运行mvn测试时,我的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\u 202”

wb1gzix0

wb1gzix01#

在你的插件配置需要这样设置;
`

...
         <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>

`
在您的示例中,标签pluguin来自plugins标签。

w8ntj3qf

w8ntj3qf2#

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

相关问题