此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

关键抽象

该框架的核心包括TestContextManagerclass 和TestContext,TestExecutionListenerSmartContextLoader接口。 一个TestContextManager为每个测试类创建(例如,用于执行JUnit Jupiter 中单个测试类中的所有测试方法)。 这TestContextManager, 反过来,管理一个TestContext它包含当前测试的上下文。 这TestContextManager还会更新TestContext随着测试的进行并将委托给TestExecutionListener实现,它通过提供依赖注入、管理事务等来检测实际的测试执行。 一个SmartContextLoader负责加载ApplicationContext对于给定的测试 类。 请参阅 javadoc 和Spring 测试套件,了解更多信息和各种实现的示例。spring-doc.cadn.net.cn

TestContext

TestContext封装运行测试的上下文(与实际使用的测试框架无关),并为它负责的测试实例提供上下文管理和缓存支持它负责的测试实例。 这TestContext也委托给SmartContextLoader加载ApplicationContext如果需要。spring-doc.cadn.net.cn

TestContextManager

TestContextManager是 Spring TestContext 框架的主要入口点,并且是负责管理单个TestContext并向每个注册的事件发出信号TestExecutionListener在定义明确的测试执行点:spring-doc.cadn.net.cn

TestExecutionListener

TestExecutionListener定义用于对 这TestContextManager侦听器注册到的。 看TestExecutionListener配置.spring-doc.cadn.net.cn

上下文加载器

ContextLoader是一个策略接口,用于加载ApplicationContext对于由 Spring TestContext 框架管理的集成测试。你应该实现SmartContextLoader而不是此接口来提供对组件类的支持,活动 Bean 定义配置文件、测试属性源、上下文层次结构和WebApplicationContext支持。spring-doc.cadn.net.cn

SmartContextLoaderContextLoader取代原始最小值ContextLoaderSPI 的 SPI 中。具体来说,一个SmartContextLoader可以选择处理资源位置、组件类或上下文初始值设定项。此外,一个SmartContextLoader可以在它加载的上下文中设置活动 Bean 定义配置文件和测试属性源。spring-doc.cadn.net.cn

Spring 提供了以下实现:spring-doc.cadn.net.cn

  • DelegatingSmartContextLoader:两个默认加载器之一,它在内部委托给 一AnnotationConfigContextLoader一个GenericXmlContextLoaderGenericGroovyXmlContextLoader,取决于为test 类声明的配置,或者是否存在默认位置或默认配置类。仅当 Groovy 位于类路径上时,才会启用 Groovy 支持。spring-doc.cadn.net.cn

  • WebDelegatingSmartContextLoader:两个默认加载器之一,它在内部委托到AnnotationConfigWebContextLoader一个GenericXmlWebContextLoaderGenericGroovyXmlWebContextLoader,取决于声明的配置测试类或是否存在默认位置或默认配置 类。 一张网ContextLoader仅在以下情况下使用@WebAppConfiguration存在于test 类上。仅当 Groovy 位于类路径上时,才会启用 Groovy 支持。spring-doc.cadn.net.cn

  • AnnotationConfigContextLoader:加载标准ApplicationContext从组件 类。spring-doc.cadn.net.cn

  • AnnotationConfigWebContextLoader:加载一个WebApplicationContext从组件 类。spring-doc.cadn.net.cn

  • GenericGroovyXmlContextLoader:加载标准ApplicationContext来自资源 位置是 Groovy 脚本或 XML 配置文件。spring-doc.cadn.net.cn

  • GenericGroovyXmlWebContextLoader:加载一个WebApplicationContext来自资源 位置是 Groovy 脚本或 XML 配置文件。spring-doc.cadn.net.cn

  • GenericXmlContextLoader:加载标准ApplicationContext从 XML 资源 地点。spring-doc.cadn.net.cn

  • GenericXmlWebContextLoader:加载一个WebApplicationContext从 XML 资源 地点。spring-doc.cadn.net.cn