使用Maven Surefire运行Cucumber JUnit5遇无测试用例检测问题求助
解决方案:Cucumber 7.8.0 + JUnit 5 测试未被Maven Surefire识别的问题
1. 升级Maven Surefire Plugin版本
maven-surefire-plugin 2.22.2版本对JUnit Platform Suite的@Suite注解支持不完善,无法识别这种方式定义的测试类。需要升级到3.0.0及以上版本,推荐使用最新稳定版:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> </plugin> </plugins> </build>
2. 清理冗余依赖
你的依赖中包含了不必要的JUnit 4兼容组件,会干扰JUnit 5的执行,需要移除以下依赖:
io.cucumber:cucumber-junit:7.8.0(这是Cucumber适配JUnit4的依赖,你用的是JUnit5平台引擎,不需要)org.junit.platform:junit-platform-runner:1.8.2(旧的JUnit平台运行器,已被Suite API替代)
同时,junit-platform-launcher和junit-platform-console如果只是通过Maven运行测试,也可以移除,Surefire会自动处理平台启动逻辑。
精简后的核心依赖应该包含:
<dependencies> <!-- Cucumber核心与JUnit5平台适配 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency> <!-- JUnit5核心 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.8.2</version> <scope>test</scope> </dependency> <!-- JUnit Platform Suite API --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-api</artifactId> <version>1.8.2</version> <scope>test</scope> </dependency> </dependencies>
3. 简化测试启动类配置
你的RunCucumberTest中同时使用了@SelectClasspathResource和FEATURES_PROPERTY_NAME配置,两者功能重复,会导致优先级冲突,保留其中一种即可。推荐保留注解方式,更简洁:
package com.example.demo; import org.junit.platform.suite.api.ConfigurationParameter; import org.junit.platform.suite.api.IncludeEngines; import org.junit.platform.suite.api.SelectClasspathResource; import org.junit.platform.suite.api.Suite; import static io.cucumber.core.options.Constants.GLUE_PROPERTY_NAME; @Suite @IncludeEngines("cucumber") @SelectClasspathResource("cucumber/mytest.feature") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.steps") public class RunCucumberTest { }
4. 验证路径正确性
- 确保feature文件路径:
src/test/resources/cucumber/mytest.feature存在,文件名和路径大小写与配置一致(类路径资源区分大小写)。 - 确保步骤定义类(Step Definitions)都在
com.example.steps包下,且类上没有错误的注解或访问修饰符问题。
内容的提问来源于stack exchange,提问作者Abraham
相关产品推荐
相关产品推荐

