对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

引导 TestContext 框架

Spring TestContext 框架内部的默认配置足以满足所有常见用例。然而,有时开发团队或第三方框架可能希望更改默认的 ContextLoader、实现自定义的 TestContextContextCache、扩充默认的 ContextCustomizerFactoryTestExecutionListener 实现集合等。为了对 TestContext 框架的运行方式实现此类底层控制,Spring 提供了一种引导(bootstrapping)策略。spring-doc.cadn.net.cn

TestContextBootstrapper 定义了用于引导测试上下文框架的 SPI。TestContextManager 使用 TestContextBootstrapper 来加载当前测试的 TestExecutionListener 实现,并构建其管理的 TestContext。您可以通过直接使用或作为元注解使用 @BootstrapWith,为测试类(或测试类层次结构)配置自定义的引导策略。如果未通过 @BootstrapWith 显式配置引导器,则将根据是否存在 @WebAppConfiguration 而使用 DefaultTestContextBootstrapperWebTestContextBootstrapperspring-doc.cadn.net.cn

由于 TestContextBootstrapper SPI 未来可能会发生变化(以适应新的需求),我们强烈建议实现者不要直接实现此接口,而应扩展 AbstractTestContextBootstrapper 或其某个具体子类。spring-doc.cadn.net.cn