Key Abstractions
该框架的核心由 TestContextManager
类和 TestContext
、TestExecutionListener
和 SmartContextLoader
接口组成。为每个测试类创建一个 TestContextManager
(例如,用于在 JUnit Jupiter 中单个测试类中的所有测试方法的执行)。反过来,TestContextManager
管理了一个 TestContext
,其中包含当前测试的上下文。TestContextManager
还随着测试的进行更新 TestContext
的状态,并委托给 TestExecutionListener
实现,该实现通过提供依赖项注入、管理事务等对实际测试执行进行设置。一个 SmartContextLoader
负责为给定的测试类加载一个 ApplicationContext
。请参阅 javadoc 和 Spring 测试套件以获取更多信息和各种实现的示例。
The core of the framework consists of the TestContextManager
class and the
TestContext
, TestExecutionListener
, and SmartContextLoader
interfaces. A
TestContextManager
is created for each test class (for example, for the execution of
all test methods within a single test class in JUnit Jupiter). The TestContextManager
,
in turn, manages a TestContext
that holds the context of the current test. The
TestContextManager
also updates the state of the TestContext
as the test progresses
and delegates to TestExecutionListener
implementations, which instrument the actual
test execution by providing dependency injection, managing transactions, and so on. A
SmartContextLoader
is responsible for loading an ApplicationContext
for a given test
class. See the javadoc and the
Spring test suite for further information and examples of various implementations.
TestContext
TestContext
封装了运行测试的环境(与正在使用的实际测试框架无关),并为其负责的测试实例提供上下文管理和缓存支持。如果需要,TestContext
也会委派一个 SmartContextLoader
加载 ApplicationContext
。
TestContext
encapsulates the context in which a test is run (agnostic of the
actual testing framework in use) and provides context management and caching support for
the test instance for which it is responsible. The TestContext
also delegates to a
SmartContextLoader
to load an ApplicationContext
if requested.
TestContextManager
TestContextManager
是 Spring TestContext 框架的主要入口点,负责管理一个 TestContext
,并在明确定义的测试执行点向每个已注册的 TestExecutionListener
发出事件:
TestContextManager
is the main entry point into the Spring TestContext Framework and is
responsible for managing a single TestContext
and signaling events to each registered
TestExecutionListener
at well-defined test execution points:
-
Prior to any “before class” or “before all” methods of a particular testing framework.
-
Test instance post-processing.
-
Prior to any “before” or “before each” methods of a particular testing framework.
-
Immediately before execution of the test method but after test setup.
-
Immediately after execution of the test method but before test tear down.
-
After any “after” or “after each” methods of a particular testing framework.
-
After any “after class” or “after all” methods of a particular testing framework.
TestExecutionListener
TestExecutionListener
定义了对 TestContextManager
发布的测试执行事件做出反应的 API,监听器使用该 API 进行注册。请参见 TestExecutionListener
Configuration。
TestExecutionListener
defines the API for reacting to test-execution events published by
the TestContextManager
with which the listener is registered. See TestExecutionListener
Configuration.
Context Loaders
ContextLoader
是一个策略接口,用于为 Spring TestContext 框架管理的集成测试加载 ApplicationContext
。您应该实现 SmartContextLoader
而不是此接口,以提供对组件类、活动 Bean 定义配置文件、测试属性来源、上下文层次结构和 WebApplicationContext
支持的支持。
ContextLoader
is a strategy interface for loading an ApplicationContext
for an
integration test managed by the Spring TestContext Framework. You should implement
SmartContextLoader
instead of this interface to provide support for component classes,
active bean definition profiles, test property sources, context hierarchies, and
WebApplicationContext
support.
SmartContextLoader
是 ContextLoader
接口的扩展,它取代了原始的最小 ContextLoader
SPI。具体而言,SmartContextLoader
可以选择处理资源位置、组件类或上下文初始化器。此外,SmartContextLoader
可以在其加载的上下文中设置活动 Bean 定义配置文件和测试属性来源。
SmartContextLoader
is an extension of the ContextLoader
interface that supersedes the
original minimal ContextLoader
SPI. Specifically, a SmartContextLoader
can choose to
process resource locations, component classes, or context initializers. Furthermore, a
SmartContextLoader
can set active bean definition profiles and test property sources in
the context that it loads.
Spring 提供以下实现:
Spring provides the following implementations:
-
DelegatingSmartContextLoader
: One of two default loaders, it delegates internally to anAnnotationConfigContextLoader
, aGenericXmlContextLoader
, or aGenericGroovyXmlContextLoader
, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. Groovy support is enabled only if Groovy is on the classpath. -
WebDelegatingSmartContextLoader
: One of two default loaders, it delegates internally to anAnnotationConfigWebContextLoader
, aGenericXmlWebContextLoader
, or aGenericGroovyXmlWebContextLoader
, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. A webContextLoader
is used only if@WebAppConfiguration
is present on the test class. Groovy support is enabled only if Groovy is on the classpath. -
AnnotationConfigContextLoader
: Loads a standardApplicationContext
from component classes. -
AnnotationConfigWebContextLoader
: Loads aWebApplicationContext
from component classes. -
GenericGroovyXmlContextLoader
: Loads a standardApplicationContext
from resource locations that are either Groovy scripts or XML configuration files. -
GenericGroovyXmlWebContextLoader
: Loads aWebApplicationContext
from resource locations that are either Groovy scripts or XML configuration files. -
GenericXmlContextLoader
: Loads a standardApplicationContext
from XML resource locations. -
GenericXmlWebContextLoader
: Loads aWebApplicationContext
from XML resource locations.