跳到主要内容
版本:7.0.3

关键抽象概念

Hunyuan 7b 中英对照 Key Abstractions

该框架的核心由TestContextManager类以及TestContextTestExecutionListenerSmartContextLoader接口组成。对于每个测试类,都会创建一个TestContextManager(例如,在JUnit Jupiter中,用于执行单个测试类内的所有测试方法)。TestContextManager负责管理一个TestContext,该TestContext保存着当前测试的上下文信息。随着测试的进行,TestContextManager还会更新TestContext的状态,并将相关任务委托给TestExecutionListener的实现类,这些实现类通过提供依赖注入、管理事务等功能来实际执行测试。SmartContextLoader则负责为特定的测试类加载ApplicationContext。有关更多信息及各种实现的示例,请参阅javadoc以及Spring测试套件。

TestContext

TestContext 封装了测试运行所处的环境(与实际使用的测试框架无关),并为其所负责的测试实例提供上下文管理和缓存支持。如果需要,TestContext 还会委托给 SmartContextLoader 来加载 ApplicationContext

TestContextManager

TestContextManager 是 Spring TestContext 框架的主要入口点,负责管理一个唯一的 TestContext,并在预定义的测试执行点向每个注册的 TestExecutionListener 发送事件:

  • 在特定测试框架的任何“before class”或“before all”方法执行之前。

  • 测试实例的后处理阶段。

  • 在特定测试框架的任何“before”或“before each”方法执行之前。

  • 在测试方法执行之前,但在测试设置之后。

  • 在测试方法执行之后,但在测试销毁之前。

  • 在特定测试框架的任何“after”或“after each”方法执行之后。

  • 在特定测试框架的任何“after class”或“after all”方法执行之后。

TestExecutionListener

TestExecutionListener 定义了用于响应由监听器注册的 TestContextManager 发布的测试执行事件的 API。请参阅 TestExecutionListener 配置

上下文加载器

ContextLoader 是一个用于加载 ApplicationContext 的策略接口,由 Spring TestContext 框架管理,用于集成测试。为了支持组件类、激活的 Bean 定义配置文件、测试属性源、上下文层次结构以及 WebApplicationContext 支持,你应该实现 SmartContextLoader 而不是这个接口。

SmartContextLoaderContextLoader 接口的扩展,它取代了原始的简化的 ContextLoader SPI。具体来说,SmartContextLoader 可以选择处理资源位置、组件类或上下文初始化器。此外,SmartContextLoader 还可以设置在其加载的上下文中的活动bean定义配置文件和测试属性源。

Spring提供了以下实现:

  • DelegatingSmartContextLoader:这是两种默认加载器之一,它会根据测试类中声明的配置,或者是否存在默认位置或默认配置类,内部委托给AnnotationConfigContextLoaderGenericXmlContextLoaderGenericGroovyXmlContextLoader进行加载。只有在类路径上包含Groovy时,才会启用Groovy支持。

  • WebDelegatingSmartContextLoader:这是两种默认加载器之一,它会根据测试类中声明的配置,或者是否存在默认位置或默认配置类,内部委托给AnnotationConfigWebContextLoaderGenericXmlWebContextLoaderGenericGroovyXmlWebContextLoader进行加载。只有当测试类上存在@WebAppConfiguration注解时,才会使用Web ContextLoader。只有在类路径上包含Groovy时,才会启用Groovy支持。

  • AnnotationConfigContextLoader:从组件类中加载标准的ApplicationContext

  • AnnotationConfigWebContextLoader:从组件类中加载WebApplicationContext

  • GenericGroovyXmlContextLoader:从Groovy脚本或XML配置文件所在的资源位置加载标准的ApplicationContext

  • GenericGroovyXmlWebContextLoader:从Groovy脚本或XML配置文件所在的资源位置加载WebApplicationContext

  • GenericXmlContextLoader:从XML资源位置加载标准的ApplicationContext

  • GenericXmlWebContextLoader:从XML资源位置加载WebApplicationContext