Bootstrapping the TestContext Framework

Spring TestContext Framework 内部默认配置足以满足所有常见用例。但是,有时开发团队或第三方框架需要更改默认 “ContextLoader”,实现自定义 “TestContext” 或 “ContextCache”,扩充默认的 “ContextCustomizerFactory” 和 “TestExecutionListener” 实现集等等。为了像这样深入控制 TestContext 框架的操作,Spring 提供了一种引导策略。

“TestContextBootstrapper” 定义了用于引导 TestContext 框架的 SPI。“TestContextManager” 使用 “TestContextBootstrapper” 为当前测试加载 “TestExecutionListener” 实现,并构建其管理的 “TestContext”。你可以使用 “@BootstrapWith” 为测试类(或测试类层次结构)配置自定义引导策略,直接使用或作为元注释。如果没有使用 “@BootstrapWith” 显式配置引导程序,则根据是否存在 “@WebAppConfiguration” 使用 “DefaultTestContextBootstrapper” 或 “WebTestContextBootstrapper”。

由于 TestContextBootstrapper SPI 未来可能会发生变化(以满足新要求),我们强烈建议实现者不要直接实现此接口,而是扩展 “AbstractTestContextBootstrapper” 或其其中一个具体子类。