我有一个黄瓜的弹簧引导应用程序。在调用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 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() { ... }
}发布于 2020-01-10 10:34:50
在使用同一个容器将依赖项注入到测试中时,不能测试依赖项注入容器。注入依赖项要求已经刷新了应用程序上下文。
因此,您必须手动创建ApplicationContext的实例,并亲自管理它的生命周期。
https://stackoverflow.com/questions/59670450
复制相似问题