我正在寻找一种为我的 cucumber 测试激活 Spring 配置文件的好方法。 cucumber 测试需要使用标记有以下内容的 stub 版本的服务:
@Profile("test")
@Component
class FooServiceStub extends FooService {...}
常规服务如下所示:
@Profile("prod")
@Component
class FooService {...}
我的要求:
- 使用 mvn 运行 cucumber 测试:$ mvn test
- 在 IDE 中运行 cucumber 测试
- 在构建服务器上运行 cucumber 测试
- 无需使用 -Dspring.profiles.active=... 参数
我找到但没有解决我的问题的资源:
- http://www.baeldung.com/cucumber-spring-integration (使用 @ContextConfiguration 加载器和 SpringApplicationContextLoader.class,在撰写本文时最新版本的 Spring Boot 1.5.2.RELEASE 中不存在。)
- programatically set Spring profile in Cucumber (搞乱系统属性)
最佳答案
在最近的 Cucumber 版本 (6.11.0
) 中,你需要一个带有 spring 配置的类,你必须注释那个类,而不是步骤定义
@ActiveProfiles("test")
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CucumberSpringConfiguration {
}
关于spring - 如何用 cucumber 激活 Spring 启动配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203301/