Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 黄瓜+;spring:如何通过测试触发SmartLifecycle事件?_Java_Spring Boot_Cucumber - Fatal编程技术网

Java 黄瓜+;spring:如何通过测试触发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

我有一个带有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 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
的实例,并自己管理它的生命周期