SpringBootTest:如何知道启动应用程序何时完成
SpringBootTest: how to know when boot application is done
Spring 引导集成测试如下所示
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application)
class IntegrationTest {
static QpidRunner qpidRunner
@BeforeClass
static void init() {
qpidRunner = new QpidRunner()
qpidRunner.start()
}
@AfterClass
static void tearDown() {
qpidRunner.stop()
}
}
因此,Qpid 实例之前 运行 并在所有测试之后被拆除。我想知道有没有办法在调用 qpidRunner.stop()
之前检查 spring 启动应用程序是否仍然 运行ning。只有当我确定 spring 应用程序已完成停止时,我才想停止 Qpid。
Spring 启动集成测试可以配置一个 ApplicationListener
来侦听 ContextClosedEvent
。在测试 class 中定义一个嵌套的 @TestConfiguration
class 以将 beans 添加到应用程序的主要配置中。
@TestConfiguration
static class MyConfiguration {
@Bean
public ApplicationListener<ContextClosedEvent> contextClosedEventListener() {
return event -> qpidRunner.stop();
}
}
考虑到 ConfigurableWebApplicationContext
可以在 SpringBootTest
中注入,将这行代码添加到代码中解决了问题
static ConfigurableWebApplicationContext context
@Autowired
void setContext(ConfigurableWebApplicationContext context) {
AbstractDocsIntegrationTest.context = context
}
@AfterClass
static void tearDown() {
context.stop()
qpidRunner.stop()
}
Spring 关于 stop method
的文档
Stop this component, typically in a synchronous fashion, such that the
component is fully stopped upon return of this method.
JUnit AfterClass
注释方法必须是静态的,因此 @Autowired
使用 setContext 方法解决方法。
Spring 引导集成测试如下所示
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application)
class IntegrationTest {
static QpidRunner qpidRunner
@BeforeClass
static void init() {
qpidRunner = new QpidRunner()
qpidRunner.start()
}
@AfterClass
static void tearDown() {
qpidRunner.stop()
}
}
因此,Qpid 实例之前 运行 并在所有测试之后被拆除。我想知道有没有办法在调用 qpidRunner.stop()
之前检查 spring 启动应用程序是否仍然 运行ning。只有当我确定 spring 应用程序已完成停止时,我才想停止 Qpid。
Spring 启动集成测试可以配置一个 ApplicationListener
来侦听 ContextClosedEvent
。在测试 class 中定义一个嵌套的 @TestConfiguration
class 以将 beans 添加到应用程序的主要配置中。
@TestConfiguration
static class MyConfiguration {
@Bean
public ApplicationListener<ContextClosedEvent> contextClosedEventListener() {
return event -> qpidRunner.stop();
}
}
考虑到 ConfigurableWebApplicationContext
可以在 SpringBootTest
中注入,将这行代码添加到代码中解决了问题
static ConfigurableWebApplicationContext context
@Autowired
void setContext(ConfigurableWebApplicationContext context) {
AbstractDocsIntegrationTest.context = context
}
@AfterClass
static void tearDown() {
context.stop()
qpidRunner.stop()
}
Spring 关于 stop method
的文档Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
JUnit AfterClass
注释方法必须是静态的,因此 @Autowired
使用 setContext 方法解决方法。