在JaCoCo maven插件中,如果Powermock Mockito的@PrepareForTest无法正确给出覆盖范围,可以考虑使用JaCoCo的exclude和include选项来手动指定需要覆盖的代码范围。
<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>
<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需要覆盖。
这样,通过手动配置exclude和include选项,可以解决Powermock Mockito的@PrepareForTest在JaCoCo maven插件中无法正确给出覆盖范围的问题。
领取专属 10元无门槛券
手把手带您无忧上云