Java 黄瓜+;spring:如何通过测试触发SmartLifecycle事件?
我有一个带有cucumber的spring boot应用程序。我希望在调用bean的start SmartLifecycle事件之前在测试中执行某些事件Java 黄瓜+;spring:如何通过测试触发SmartLifecycle事件?,java,spring-boot,cucumber,Java,Spring Boot,Cucumber,我有一个带有cucumber的spring boot应用程序。我希望在调用bean的start SmartLifecycle事件之前在测试中执行某些事件 Given Something that needs to happen before beans are started And Beans start up now Then Life is good 有没有办法做到这一点? 默认情况下,它看起来像是Spring在执行任何Cucumber语句之前初始化并启动所有bean 示例: class
Given Something that needs to happen before beans are started
And Beans start up now
Then Life is good
有没有办法做到这一点?
默认情况下,它看起来像是Spring在执行任何Cucumber语句之前初始化并启动所有bean
示例:
class Context {
@Bean
SomeBean someBean() { return new SomeBean(); }
}
class SomeBean implements SmartLifecycle {
@Override
void start() {
// some meaningful work that depends on setup that needs to be done beforehand
}
// rest of interface implementation
}
@ContextConfiguration(classes = Context.class)
class CucumberFeatures {
@Autowired
private SomeBean someBean;
@Given("Something that needs to happen before beans are started")
public void something() {
// ...
}
@Given("Beans start up now")
public void beansStarted() {
// This should start beans in their defined order now
}
@Then("Life is good")
public void lifeIsGood() { ... }
}
黄瓜定义文件:
class Context {
@Bean
SomeBean someBean() { return new SomeBean(); }
}
class SomeBean implements SmartLifecycle {
@Override
void start() {
// some meaningful work that depends on setup that needs to be done beforehand
}
// rest of interface implementation
}
@ContextConfiguration(classes = Context.class)
class CucumberFeatures {
@Autowired
private SomeBean someBean;
@Given("Something that needs to happen before beans are started")
public void something() {
// ...
}
@Given("Beans start up now")
public void beansStarted() {
// This should start beans in their defined order now
}
@Then("Life is good")
public void lifeIsGood() { ... }
}
当使用同一容器将依赖项注入测试时,不能测试依赖项注入容器。注入依赖项要求已刷新应用程序上下文 因此,您必须手动创建
ApplicationContext
的实例,并自己管理它的生命周期