首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Powermock Mockito的@PrepareForTest在JaCoCo maven插件中没有给出正确的覆盖范围。有其他选择吗?

在JaCoCo maven插件中,如果Powermock Mockito的@PrepareForTest无法正确给出覆盖范围,可以考虑使用JaCoCo的exclude和include选项来手动指定需要覆盖的代码范围。

  1. 首先,需要在pom.xml文件中添加JaCoCo插件的配置:
代码语言:txt
复制
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 接下来,在插件配置中添加exclude和include选项来指定需要覆盖的代码范围。exclude用于排除不需要覆盖的代码,include用于指定需要覆盖的代码。
代码语言:txt
复制
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>com/example/TestClass1</exclude>
                    <exclude>com/example/TestClass2</exclude>
                </excludes>
                <includes>
                    <include>com/example/TestClass3</include>
                    <include>com/example/TestClass4</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

在上述配置中,我们通过exclude排除了TestClass1和TestClass2,通过include指定了TestClass3和TestClass4需要覆盖。

  1. 运行测试时,JaCoCo插件将根据配置的exclude和include选项来计算代码覆盖率。

这样,通过手动配置exclude和include选项,可以解决Powermock Mockito的@PrepareForTest在JaCoCo maven插件中无法正确给出覆盖范围的问题。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

1分10秒

DC电源模块宽电压输入和输出的问题

5分33秒

JSP 在线学习系统myeclipse开发mysql数据库web结构java编程

领券