JUnit4升级JUnit5时RunTest类遇Cucumber特性未找到警告
解决JUnit 5 Cucumber升级后"No features found in package 'automation'"警告
问题根源
- 错误的资源路径:
@SelectClasspathResource基于类路径(classpath)查找资源,src/test/resources本身就是测试类路径的根目录,无需在路径中包含它。原写法会导致Cucumber在classpath下寻找不存在的src/test/resources/features路径,转而尝试在当前类的包(automation)下查找features,触发警告。 - 缺失Glue配置:JUnit 4中的
glue参数在JUnit 5中需要通过@ConfigurationParameter单独配置,否则Cucumber无法找到步骤定义和钩子类。 - 未导入常量:
PLUGIN_PROPERTY_NAME是Cucumber提供的常量,未正确导入会导致编译报错。 - 无用注解:
@TestMethodOrder是JUnit Jupiter控制测试方法顺序的注解,对Cucumber Suite类无效,应移除。
修正后的代码
package automation; import org.junit.platform.suite.api.ConfigurationParameter; import org.junit.platform.suite.api.IncludeTags; import org.junit.platform.suite.api.SelectClasspathResource; import org.junit.platform.suite.api.Suite; import io.cucumber.junit.platform.engine.Cucumber; import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; @Suite @Cucumber @SelectClasspathResource("features") // 修正:直接使用类路径下的features目录 @IncludeTags("WIP") @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty, json:target/cucumber-json-report.json, br.com.dimensa.hooks.TestEventHandlerPlugin") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "br.com.dimensa.hooks, utils, automation.steps") // 补充Glue配置 public class RunTest { }
关键修改说明
- 调整
@SelectClasspathResource路径为"features",匹配classpath下的features目录(对应原项目的src/test/resources/features)。 - 导入
io.cucumber.junit.platform.engine.Constants中的常量,避免硬编码配置键名。 - 添加
GLUE_PROPERTY_NAME配置,对应JUnit 4中的glue参数,确保Cucumber能找到步骤定义和钩子类。 - 移除无用的
@TestMethodOrder注解。
额外验证
- 确认
src/test/resources/features目录下存在.feature文件。 - 确认Glue配置的包路径正确覆盖所有步骤定义和钩子类所在位置。
内容的提问来源于stack exchange,提问作者Lucas Tenório
相关产品推荐
相关产品推荐

