如何在 Maven 测试阶段生成 jacoco 报告
Posted
技术标签:
【中文标题】如何在 Maven 测试阶段生成 jacoco 报告【英文标题】:How to generate jacoco report in maven test phase 【发布时间】:2021-12-28 05:27:58 【问题描述】:我在单元测试用例中使用了 Mockito 和 Power Mockito。当我运行配置文件代码覆盖率时,我能够生成 jacoco 报告,但是
我在测试阶段尝试生成 Jacoco 报告时出错
错误 [错误] 无法在项目 testproject-api 上执行目标 org.jacoco:jacoco-maven-plugin:0.8.2:report (default-report):JaCoCo 报告生成时出错。:创建报告时出错:出错时分析 d:\workspace\api\target\classes\pkg\ResponseBuilder.class。无法处理检测类 pkg\ResponseBuilder.class。请提供原始的非仪器类。 -> [帮助 1]
代码 请在下面找到代码
<profiles>
<profile>
<id>code-coverage</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>$jacocoVersion</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>$jacocoVersion</version>
<executions>
<!-- Off line instrumentation is needed to compute coverage for Power Mock tests -->
<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>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>$maven.surefire.plugin.version</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>$maven.surefire.plugin.version</version>
</dependency>
</dependencies>
<configuration>
<!-- Workaround to https://code.google.com/p/powermock/issues/detail?id=504 -->
<argLine>-XX:-UseSplitVerifier</argLine>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
</plugins>
</build>
</profile>
【问题讨论】:
【参考方案1】:问题现已解决。我需要为 default-restore-instrumented-classes 目标添加测试阶段。请找到更新后的代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>$maven.surefire.plugin.version</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>$maven.surefire.plugin.version</version>
</dependency>
</dependencies>
<configuration>
<!-- Workaround to https://code.google.com/p/powermock/issues/detail?id=504 -->
<argLine>-XX:-UseSplitVerifier</argLine>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>$jacocoVersion</version>
<executions>
<execution>
<id>coverage-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<phase>test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
【讨论】:
以上是关于如何在 Maven 测试阶段生成 jacoco 报告的主要内容,如果未能解决你的问题,请参考以下文章
安卓项目中有maven库的依赖(aar/jar)。如何使用jacoco测试依赖库的覆盖率呢
jacoco+maven+sonar+springboot 单元测试代码覆盖率统计