【发布时间】:2020-09-17 10:55:09
【问题描述】:
当使用SpringExtension 时,我需要在所有测试(在所有类中)之前/之后执行一些初始化/取消初始化代码。我尝试了两种解决方案:
@ExtendWith({BeforeAllAfterAll.class, SpringExtension.class})
@ContextConfiguration(classes = ContextConfig.class)
public class ServiceIT{..}
public class BeforeAllAfterAll implements BeforeAllCallback,
ExtensionContext.Store.CloseableResource {...}
但是,在 BeforeAllAfterAll 类中,我无法获得对应用程序上下文的引用。
我尝试使用事件:
public class ContextEventHandler {
@EventListener
public void handleContextStartedEvent(ContextStartedEvent event) {
System.out.println("Context Start Event received.");
}
@EventListener
public void handleContextRefreshEvent(ContextRefreshedEvent event) {
System.out.println("Context Refreshed Event received.");
}
@EventListener
public void handleContextStoppedEvent(ContextStoppedEvent event) {
System.out.println("Context Stopped Event received.");
}
}
但是,正如我在使用 SpringExtension 时发现的那样,StartedEvent 和 StoppedEvent 不会被触发。
谁能告诉我怎么做?我使用 Spring5 和 JUnit5。
【问题讨论】: