【发布时间】:2019-06-03 14:31:39
【问题描述】:
我写了一个ApplicationListener,它应该检查环境是否在上下文初始化期间准备好。我在测试场景时遇到了麻烦,因为我在 configure() 和 main() 方法中都手动添加了侦听器。
ApplicationListener 类:
public class EnvironmentPrepared implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
//code that checks if conditions are met
if (checkTrue) {
throw new RuntimeException();
}
}
}
主类:
public class MyApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
setRegisterErrorPageFilter(false);
return application.listeners(new EnvironmentPrepared()).sources(MyApp.class);
}
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MyApp.class);
springApplication.addListeners(new EnvironmentPrepared());
springApplication.run(args);
}
}
我要执行的测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(loader = OverriddenProfilesTest.CustomLoader.class)
public class OverriddenProfilesTest {
public static class CustomLoader extends SpringBootContextLoader {
@Override
protected SpringApplication getSpringApplication() {
SpringApplication app = super.getSpringApplication();
app.addListeners(new EnvironmentPrepared());
return app;
}
}
/**
* Checks if spring can bootstrap everything
*/
@Test(expected = RuntimeException.class)
public void test() {
}
}
这将是我想要的测试。 RuntimeException 被抛出,但异常发生在上下文初始化期间,因此测试甚至没有开始。
【问题讨论】:
-
你可以尝试在你的测试方法中以编程方式加载它,检查
AbstractApplicationContext的层次结构,对于xml配置例如:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("my-app-context.xml"); -
感谢您的输入,我已经使用 AbstractRefreshableApplicationContext 进行了尝试,但没有达到我的预期。在我看来,通常创建另一个上下文会产生更多问题。您可以查看我在下面使用的解决方案...
标签: spring-boot integration-testing listener spring-boot-test