如何解决Surefire测试跳过与JaCoCo缺失执行数据文件报错问题
问题:执行
mvn test时测试被跳过,无法生成JaCoCo覆盖率报告 我想要生成JaCoCo测试覆盖率报告,但执行mvn test命令时,Maven直接跳过了测试环节,导致报告无法生成。控制台输出如下:
--- jacoco-maven-plugin:0.8.8:prepare-agent (prepare-agent) @ xx --- [INFO] argLine set to -javaagent:C:\xxx\xx\.m2\repository\org\jacoco\org.jacoco.agent\0.8.8\org.jacoco.agent-0.8.8-runtime.jar=destfile=C:\xx\xx\git\xx-xx\target\jacoco.exec [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xx --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 16 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ xx --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 33 source files to C:\xx\xx\git\xx-xx\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xx --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 6 resources [INFO] Copying 20 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xx --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 16 source files to C:\xx\xx\git\xx-xx\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ xx --- [INFO] Tests are skipped. [INFO] [INFO] --- jacoco-maven-plugin:0.8.8:report (report) @ xx --- [INFO] Skipping JaCoCo execution due to missing execution data file. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.570 s [INFO] Finished at: 2023-08-22T15:42:57+02:00 [INFO] ------------------------------------------------------------------------ Process finished with exit code 0
我已经多次调整pom.xml的配置(相关片段如下,部分尝试过的配置已注释),但问题仍未解决,StackOverflow上的多数方案也试过了,都没用。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.1.2</version> </plugin> <!--<plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.7.0.1746</version> </plugin>--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <!--<version>3.0.0-M5</version>--> <version>3.0.0-M5</version> <configuration> <skipTests>false</skipTests> <includes> <include>/**/*Test</include> <!-- <include> **/*test/java/*</include> --> </includes> </configuration> <executions> <execution> <id>default-test</id> <configuration> <skip>false</skip> </configuration> </execution> </executions> <!--<configuration> <testFailureIgnore>false</testFailureIgnore> <argLine>-Xmx1024M ${argLine}</argLine> <forkedProcessExitTimeoutInSeconds>240</forkedProcessExitTimeoutInSeconds> </configuration>--> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.8</version> <!-- <configuration> <excludes> <exclude>**/*test/java/*</exclude> <exclude>**/*test/resources/*</exclude> </excludes> </configuration>--> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <!-- <phase>prepare-package</phase>--> <goals> <goal>report</goal> </goals> <phase>test</phase> </execution> <!-- <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.60</minimum> </limit> </limits> </rule> </rules> </configuration> </execution>--> </executions> </plugin> </plugins> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory>
解决方案
按优先级尝试以下步骤:
修正Surefire测试类匹配规则
当前配置的<include>/**/*Test</include>格式错误,Maven路径匹配需遵循Ant风格,正确写法为:<includes> <include>**/*Test.java</include> </includes>需添加
.java后缀,且路径开头无需斜杠,**代表任意层级目录。强制关闭跳过测试
执行命令时显式覆盖可能存在的全局或隐式配置:mvn test -DskipTests=false -Dmaven.test.skip=false同时检查项目或用户Maven配置目录下的
settings.xml,确保没有全局设置skipTests为true。清理缓存后重新构建
执行mvn clean test,先清理target目录,避免旧编译文件干扰测试执行。简化Surefire插件配置
移除冗余的executions块,简化为:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <skipTests>false</skipTests> <includes> <include>**/*Test.java</include> </includes> </configuration> </plugin>检查测试类规范
确保测试类放在src/test/java目录下,类名以Test结尾,且测试方法使用@Test注解标记。调整JaCoCo报告阶段
将JaCoCo的report阶段绑定到prepare-package,确保测试完成后再生成报告:<execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution>
内容的提问来源于stack exchange,提问作者Jeet
相关产品推荐
相关产品推荐

