2024-03-28
557
15 MIN READ
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>0.8.7</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<configuration>
<excludes>
<exclude>com/baidu/config/*</exclude>
</excludes>
<!-- Path to generate .exec file -->
<dataFile>${project.basedir}/target/coverage/jacoco.exec</dataFile>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.basedir}/target/coverage/jacoco.exec</destFile>
</configuration>
</execution>
<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-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/target/coverage/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<skipTests>false</skipTests>
<testSourceDirectory>src/test/java/**/*</testSourceDirectory>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.basedir}/target/coverage/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
在进行 mvn clean install
的时候,Maven Surefire 跳过执行所有测试时,需要在 Maven Surefire 插件处配置:
<configuration>
<skipTests>false</skipTests>
</configuration>
在进行 mvn clean install
的时候,当 JaCoCo 提示这个异常:Skipping JaCoCo execution due to missing execution data file
,这说明 JaCoCo 没能成功链接到 jacoco.exec
执行文件,此时需要检查 JaCoCo 插件的配置。
首先需要配置准备代理:
<execution>
<id>prepare-unit-tests</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.basedir}/target/coverage/jacoco.exec</destFile>
</configuration>
</execution>
其次是生成报告:
<execution>
<id>default-report</id>
<!-- <phase>prepare-package</phase>-->
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/target/coverage/jacoco.exec</dataFile>
</configuration>
</execution>
其中 <phase></phase>
可以不用写,这时会使用默认的值。
由于两个框架都是对测试类进行代理处理,这会导致在 JaCoCo 扫描测试类时用到的代理类和 PowerMockito 生成的代理类不同,使得 JaCoCo 生成的测试报告覆盖率异常,这个时候我们需要使用 JaCoCo 的离线模式:
<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>
需要注意的是:这种情况下需要在 <dependencies>
中显式标注对 JaCoCo 的依赖:
<dependency>
<!-- must be on the classpath -->
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>0.8.12-SNAPSHOT</version>
<scope>test</scope>
</dependency>
参考文献 JaCoCo 官方文档