如何在运行时使用Cucumber而不是通过测试加载Spring Boot应用程序上下文



我正在使用1.5.8.RELEASE开发一个Spring Boot应用程序,该应用程序是通过CLI配置和运行的。这些参数用于将应用程序配置为以一种或多种不同的方式运行。其中一个命令是运行所有黄瓜功能,并使用以下命令调用:

cucumber.api.cli.Main.main(cucumberArguments);

当前传递的参数包括:featureFolder-gstepsPackage和几个黄瓜报表配置参数。

我希望能够通过我的application.properties文件或特定于配置文件的applications.properties文件在我的CucumberSteps*.java文件中配置一些属性,但我目前运行这些功能的方式意味着没有加载Spring Boot上下文。


我的第一次尝试是:

@ConfigurationProperties
public class CucumberStepsFeature1 {
@Value("${my.property}")
private String myProperty;
@Given("...")
public void given() {
// fails below
assertNotNull(this.myProperty);
}
}

我第二次尝试解决这个问题是:

@ContextConfiguration(classes = MyMainApp.class)
@ConfigurationProperties
public class CucumberStepsFeature1 {
@Value("${my.property}")
private String myProperty;
@Given("...")
public void given() {
// fails below
assertNotNull(this.myProperty);
}
}

但是我收到的错误信息

Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication

我试着按照这里列出的步骤操作,但没有成功。


如果有任何帮助,我将不胜感激,但我现在要注意的是,由于公司政策的原因,我在这里分享的内容将非常有限。我将无法复制或粘贴任何真实的代码片段或日志。

下面是我能找到的实现我想要的东西的最干净的方法:

创建一个Config类以加载到您需要的Spring应用程序属性(application.properties和/或application-${PROFILE}.properties)中,例如:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
public class PropsConfig {
@Value("${my.property}")
private String myProperty;
//any other properties...
public String getMyProperty() { 
return this.myProperty; 
}

创建另一个类作为Spring ApplicationContext的容器。通过将这个类放在主应用程序类的子包中,确保Spring扫描了它。参见以下代码:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringApplicationContextContainer implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Autowired
public SpringApplicationContextContainer(ApplicationContext applicationContext) {
SpringApplicationContextContainer.applicationContext = applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringApplicationContextContainer.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return SpringApplicationContextContainer.applicationContext;
}
}

最后,在CucumberSteps类或任何其他非Spring类中,您可以简单地调用SpringApplicationContextContainer.getApplicationContext();,如下所示:

public class CucumberStepsFeature1 {
private final PropsConfig propsConfig;
public CucumberStepsFeature1() {
this.propsConfig = SpringApplicationContextContainer.getApplicationContext().getBean(PropsConfig.class);
}
@Given("...")
public void given() {
// passes below
assertNotNull(this.propsConfig);
}

最新更新