Maven Failsafe无法识别Cucumber集成测试问题求助
问题
使用Micronaut、Cucumber、JUnit5编写集成测试,将测试类命名为FooIT后,执行mvn verify时Maven Failsafe插件能找到该类,但提示:
FooIT » NoTestsDiscovered Suite [foo.FooIT] did not discover any tests
但直接运行Cucumber测试时一切正常。
相关代码
测试类
@MicronautCucumber public class FooIT { }
自定义@MicronautCucumber注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Inherited @Documented @Testable @Suite @IncludeEngines("cucumber") @SelectClasspathResource("features") @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty") @ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long") @ConfigurationParameter(key = PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true") // Add some tests to class @SuppressWarnings("java:S2187") public @interface MicronautCucumber { }
Failsafe插件配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
解决方案
1. 配置Failsafe启用JUnit Platform支持
Failsafe 3.x需要明确配置以支持JUnit Platform Suite,修改插件配置添加<configuration>节点:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <!-- 启用JUnit Platform模块支持 --> <useModule>true</useModule> <!-- 明确指定集成测试类匹配规则 --> <includes> <include>**/*IT.java</include> </includes> </configuration> </plugin>
2. 验证特性文件路径与类路径
确保src/test/resources/features目录存在且包含.feature格式的测试文件,Failsafe运行时需要这些资源被正确加载到测试类路径中。
3. 确认依赖版本兼容性
确保Cucumber、JUnit 5、Micronaut的依赖版本互相兼容,添加或更新以下依赖(版本根据实际项目调整):
<!-- Cucumber JUnit Platform引擎 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>7.14.0</version> <scope>test</scope> </dependency> <!-- JUnit Platform Suite API --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-api</artifactId> <version>1.10.0</version> <scope>test</scope> </dependency> <!-- Micronaut JUnit5测试支持 --> <dependency> <groupId>io.micronaut.test</groupId> <artifactId>micronaut-test-junit5</artifactId> <scope>test</scope> </dependency>
4. 验证测试执行
执行mvn clean verify,检查Failsafe是否能正常发现并运行Cucumber测试用例。
内容的提问来源于stack exchange,提问作者Michael Resnick
相关产品推荐
相关产品推荐

