【问题标题】:How to programmatically set Spring profile in Cucumber如何以编程方式在 Cucumber 中设置 Spring 配置文件
【发布时间】:2014-03-24 14:35:34
【问题描述】:

我已经开始为我的 Spring 应用程序使用黄瓜测试。我通过传递 JVM 参数 spring.profiles.active=testProfile 来设置活动 Spring 配置文件。

有没有办法以编程方式做到这一点?比如:

@RunWith(Cucumber.class)
@Cucumber.Options(...)
@ActiveProfiles("testProfile")
public class MyCucumberTest

使用黄瓜 1.1.6

【问题讨论】:

  • 简单地放入一个静态块怎么样。公共类 MyCucumberTest{static{System.setproperty("spring.profiles.active","testProfile")}}

标签: spring cucumber profile


【解决方案1】:

我不确定这是不是正确的方法,但至少它可以为我工作。我正在使用黄瓜版本 1.2.4

我专门使用 Cucumber junit runner 在构造函数中设置所需的弹簧轮廓: public class SpringProfileCucumber extends Cucumber { public SpringProfileCucumber(Class clazz) throws InitializationError, IOException { super(clazz); System.setProperty("spring.profiles.active", "testProfile"); } }

在测试中使用 SpringProfileCucumber junit runner @RunWith(SpringProfileCucumber.class) @CucumberOptions(features = {"src/test/java/tests/cucumber/test.feature"}, glue = { "tests.cucumber"}) public class MyCucumberTest { // ... the test }

【讨论】:

    【解决方案2】:

    这已经过时了。 cucumber.xml的使用不适用于cucumber-jvm。

    我也有类似的问题,尝试使用:

    @ActiveProfiles(value = "DEV", inheritProfiles = false)
    @IfProfileValue(name= "ENV", value = "DEV")
    

    使用命令行:

    -DENV=DEV -Dspring.active.profiles=DEV
    

    这些指令似乎不适用于 spring-cucumber 库(至少是 1.1.5)。

    【讨论】:

      【解决方案3】:

      扩展@perseger 的答案,以下专门的跑步者允许您使用@ActiveProfiles 注释

      public class SpringProfileCucumber extends Cucumber {
          public SpringProfileCucumber(Class clazz)
                  throws InitializationError, IOException {
              super(clazz);
              ActiveProfiles ap = (ActiveProfiles) clazz
                      .getAnnotation(ActiveProfiles.class);
              if (ap != null) {
                  System.setProperty("spring.profiles.active",
                          String.join(",", ap.value()));
              }
          }
      }
      

      在测试中,然后可以使用

      @RunWith(SpringProfileCucumber.class)
      @CucumberOptions(...)
      @ActiveProfiles("testProfile")
      public class RyvrTests {
      
      }
      

      【讨论】:

        【解决方案4】:

        您可以为 spring boot 提供一个环境变量以用作活动配置文件。使用 @BeforeClass 注释方法允许您在应用程序测试开始之前设置所需的配置文件

        @RunWith(Cucumber.class)
        @Cucumber.Options(...)
        public class MyCucumberTest {
            @BeforeClass
            public static void setUp() {
                System.setProperty("spring.profiles.active", "testProfile");
            }
        }
        

        【讨论】:

          【解决方案5】:

          或者,您也可以创建注释来激活黄瓜的配置文件。它看起来像这样:

          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 {
          }
          

          引用我对这个问题的回答:

          How to activate spring boot profile with cucumber

          【讨论】:

            猜你喜欢
            • 2015-09-24
            • 2016-09-25
            • 2017-03-09
            • 1970-01-01
            • 1970-01-01
            • 2018-03-13
            • 2011-10-02
            • 2011-04-06
            • 1970-01-01
            相关资源
            最近更新 更多