【问题标题】:Jacoco coverage percentage mavenJacoco 覆盖率专家
【发布时间】:2021-04-27 12:57:46
【问题描述】:

运行 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


    【解决方案1】:

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

    `

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

    在你的例子中,标签插件来自插件标签。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-11
        • 2019-01-02
        • 2012-11-02
        • 1970-01-01
        • 2014-11-09
        • 2018-05-28
        • 2012-06-11
        • 1970-01-01
        相关资源
        最近更新 更多