原创

JaCoCo配置与bug处理

JaCoCo配置与bug处理

1. Maven依赖

<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <classifier>runtime</classifier>
    <version>0.8.7</version>
    <scope>test</scope>
</dependency>

2. Maven插件配置

2.1 JaCoCo插件配置

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

2.2 Maven Surefire 插件配置

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

3. Bug fix

3.1 Maven Surefire 跳过执行Test

在进行 mvn clean install 的时候,Maven Surefire 跳过执行所有测试时,需要在 Maven Surefire 插件处配置:

<configuration>
    <skipTests>false</skipTests>
</configuration>

3.2 JaCoCo 提示:Skipping JaCoCo execution

在进行 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> 可以不用写,这时会使用默认的值。

3.3 在使用了PowerMockito框架的情况下,JaCoCo报告覆盖率异常

由于两个框架都是对测试类进行代理处理,这会导致在 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 官方文档

执行JaCoCo时获得“由于缺少执行数据文件而跳过JaCoCo执行”

Java
  • 作者:CoCo(联系作者)
  • 发表时间:2024-03-28 19:10:07
  • 更新时间:2024-03-28 19:10:07
  • 版权声明 © 原创不易,转载请注明出处
  • 留言