如何用黄瓜激活弹簧靴型材 [英] How to activate spring boot profile with cucumber

查看:167
本文介绍了如何用黄瓜激活弹簧靴型材的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a nice way to active a spring profile for my cucumber tests. The cucumber tests need to use a stubbed version of a service which is marked with:

@Profile("test")
@Component
class FooServiceStub extends FooService {...}

常规服务如下:

@Profile("prod")
@Component
class FooService {...}    

我的要求:

  • 使用mvn运行黄瓜测试:$ mvn test
  • 在IDE中运行黄瓜测试
  • 在构建服务器上运行黄瓜测试
  • 无需使用-Dspring.profiles.active = ...参数

我找到了但无法解决我的问题的来源:

Sources I've found but don't solve my issue:

  • http://www.baeldung.com/cucumber-spring-integration (using @ContextConfiguration loader with SpringApplicationContextLoader.class which is not present in the latest version of Spring Boot, 1.5.2.RELEASE at the time of writing.)
  • programatically set Spring profile in Cucumber (messing with system property)

推荐答案

我已经通过在FeatureStep类上添加注释来解决了这个问题.

I've solved this issue with an annotation that I put on my FeatureStep class.

注释:

注意上面的@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 {

}

使用注释:

将注释添加到功能步骤:

Adding the annotation to the feature steps:

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

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

Now when running the Cucumber feature tests, either from my IDE, from the command line with maven or on the build server, my test is using the FooServiceSTub and my tests pass.

这篇关于如何用黄瓜激活弹簧靴型材的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆