首页 > 解决方案 > Spring Cucumber 为 Bean 设置应用程序属性

问题描述

我在配置类中定义了一个 bean:

@Configuration
public class Config {
  @Value("${some-property}")
  private String someProperty;

  @Bean
  public SomeBean someBean() {
    return new SomeBean(someProperty);
  }
}

如何定义黄瓜步骤定义来设置属性some-property并让其SomeBean使用?

我面临的问题是我的 bean 在任何步骤发生之前就被初始化了。如何重新初始化 bean,或使用@RefreshScope可能刷新?或者我可以在后面的步骤中启动/重新启动 spring 上下文吗?

黄瓜步骤:

@Given("I have some property set to (.*)")
public void someProperty(String someProperty) {
  // Answer
}

这是启动 Spring Context 的空步骤定义类:

@SpringBootTest(classes = Application.class)
@DirtiesContext
@ActiveProfiles("it")
@AutoConfigureCache
@AutoConfigureTestEntityManager
@AutoConfigureWebMvc
@AutoConfigureMockMvc(secure = false)
@ImportAutoConfiguration
public class CucumberSpringContextBootstrapper implements En {
}

标签: javaspringspring-bootcucumbercucumber-java

解决方案


推荐阅读