首页 > 解决方案 > 如果发生 `Failed to load ApplicationContext` 错误,自动终止所有集成测试

问题描述

我们有一些集成测试(使用 spring 框架编写)由于bean 初始化异常而失败,最终导致Failed to load ApplicationContext. 根据我对spring testing docs的理解,加载ApplicationContext 发生在类级别,所以我的疑问是 -

  1. 一旦在集成测试类运行期间由于bean 初始化异常ApplicationContext而 失败(即),是否 会为该特定集成测试类中存在的每个单独的集成测试再次尝试启动(最终将失败)?Failed to load ApplicationContextApplicationContext

    1. 询问上述情况是因为当 bean 发生故障时我们看到与 postgres 的连接数量激增,似乎对于集成测试类中存在的每个集成测试(最终由于 失败Failed to load ApplicationContext),spring 尝试创建与 postgres 的新连接并且在失败之前不会破坏它ApplicationContext。我们如何才能阻止这种情况,请提供一些建议。
  2. 此外,一旦我们得到Failed to load ApplicationContext,有没有办法以编程方式完全自动终止所有集成测试的运行?如果是,请帮助如何实现它?谢谢。

测试框架——junit + Spring

更新:使用了提到的测试框架。

标签: javaspringspring-bootintegration-testingspring-test

解决方案


如果ApplicationContext反复加载失败,目前无法中止集成测试。

要投票支持此类支持,请参阅此 Spring Framework 问题


推荐阅读