简体   繁体   English

Jacoco 覆盖率 maven

[英]Jacoco coverage percentage maven

When runnning mvn test, my spring boot application builds succesfully.运行 mvn 测试时,我的 spring 引导应用程序构建成功。 When I do mvn clean install or mvn clean verify, build fails.当我执行 mvn clean install 或 mvn clean verify 时,构建失败。 This is because I have minimum code coverage ratio configured to 80%.这是因为我将最小代码覆盖率配置为 80%。 When I run mvn clean install/verify, the result shows instructions covered ratio is 0.00, but expected minimum is 0.50.当我运行 mvn clean install/verify 时,结果显示指令覆盖率为 0.00,但预期最小值为 0.50。

Before setting the minimum threshold, running mvn clean install, gave the below result, which says 65% covered.在设置最小阈值之前,运行 mvn clean install 会给出以下结果,即覆盖率为 65%。 What is missing in my configuration?我的配置中缺少什么? Am I missing anything?我错过了什么吗? How can I get jacoco pick the correct code covered?我怎样才能让 jacoco 选择正确的代码?

在此处输入图像描述

Here is my pom这是我的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>

I am using java version "1.8.0_202"我正在使用 java 版本“1.8.0_202”

In you's plugin config need set like that;在你的插件配置中需要这样设置;

` `

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

in your's exemple the tag pluguin is out from plugins tag.在您的示例中,标签插件来自插件标签。

If you are using Jacoco with PowerMockito, and the class which you want to cover is taken in PrepareForTest, then in that case it will show you 0% coverage.如果您将 Jacoco 与 PowerMockito 一起使用,并且您想要覆盖的 class 被记录在 PrepareForTest 中,那么在这种情况下,它将显示 0% 的覆盖率。 Try removing the target class from PrepareForTest尝试从 PrepareForTest 中删除目标 class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM