跳到主要内容

引导 TestContext 框架

ChatGPT-4o 中英对照 Bootstrapping the TestContext Framework

Spring TestContext 框架内部的默认配置足以满足所有常见的使用场景。然而,有时开发团队或第三方框架可能希望更改默认的 ContextLoader,实现自定义的 TestContextContextCache,扩展默认的 ContextCustomizerFactoryTestExecutionListener 实现集等。对于这种对 TestContext 框架操作的低级控制,Spring 提供了一种引导策略。

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

由于 TestContextBootstrapper SPI 可能会在未来发生变化(以适应新的需求),我们强烈建议实现者不要直接实现此接口,而是扩展 AbstractTestContextBootstrapper 或其具体子类之一。