小编典典

如何用cucumber激活Spring靴轮廓

spring-boot

我正在寻找一种有效的方法来激活我的黄瓜测试的弹簧曲线。cucumber测试需要使用带有以下标记的服务的存根版本:

@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中没有提供此加载器。)
  • 在Cucumber中 以编程方式设置Spring配置文件(与系统属性一起发送)

阅读 466

收藏
2020-05-30

共1个答案

小编典典

我已通过在FeatureStep类上添加注释来解决了此问题。

注释:

注意它上的@ActiveProfiles。

import java.lang.annotation.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration
@ActiveProfiles("test")
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
    classes = FeatureTestConfiguration.class)
public @interface FeatureFileSteps {
}

配置类非常基础:

@Configuration
@Import(FooApplication.class)
public class FeatureTestConfiguration {

}

使用注释:

将注释添加到功能步骤:

@FeatureFileSteps
public class FooFeatureSteps {
    @Given(...)
    @When(...)
    @Then(...)
}

现在,从我的IDE,使用maven的命令行或在构建服务器上运行Cucumber功能测试时,我的测试正在使用FooServiceSTub,并且我的测试通过了。

2020-05-30