Context Management

每个 TestContext 都为其负责的测试实例提供上下文管理和缓存支持。测试实例不会自动获取对已配置的 ApplicationContext 的访问。但是,如果测试类实现了 ApplicationContextAware 接口,ApplicationContext 的引用会提供给测试实例。请注意,AbstractJUnit4SpringContextTestsAbstractTestNGSpringContextTests 实现了 ApplicationContextAware,因此自动提供对 ApplicationContext 的访问。

Each TestContext provides context management and caching support for the test instance for which it is responsible. Test instances do not automatically receive access to the configured ApplicationContext. However, if a test class implements the ApplicationContextAware interface, a reference to the ApplicationContext is supplied to the test instance. Note that AbstractJUnit4SpringContextTests and AbstractTestNGSpringContextTests implement ApplicationContextAware and, therefore, provide access to the ApplicationContext automatically.

@Autowired ApplicationContext

作为实现 ApplicationContextAware 接口的替代方法,您可以通过 @Autowired 注释将字段或setter方法中的应用程序上下文注入到您的测试类中,如下面的示例所示:

As an alternative to implementing the ApplicationContextAware interface, you can inject the application context for your test class through the @Autowired annotation on either a field or setter method, as the following example shows:

Java
@SpringJUnitConfig
class MyTest {

	@Autowired (1)
	ApplicationContext applicationContext;

	// class body...
}
1 Injecting the ApplicationContext.
Kotlin
@SpringJUnitConfig
class MyTest {

	@Autowired (1)
	lateinit var applicationContext: ApplicationContext

	// class body...
}
2 Injecting the ApplicationContext.

类似地,如果您的测试配置为加载 WebApplicationContext,您可以将 Web 应用程序上下文注入到您的测试中,如下所示:

Similarly, if your test is configured to load a WebApplicationContext, you can inject the web application context into your test, as follows:

Java
@SpringJUnitWebConfig (1)
class MyWebAppTest {

	@Autowired (2)
	WebApplicationContext wac;

	// class body...
}
1 Configuring the WebApplicationContext.
2 Injecting the WebApplicationContext.
Kotlin
@SpringJUnitWebConfig (1)
class MyWebAppTest {

	@Autowired (2)
	lateinit var wac: WebApplicationContext
	// class body...
}
3 Configuring the WebApplicationContext.
4 Injecting the WebApplicationContext.

使用 @Autowired 进行依赖项注入是由默认配置的 `DependencyInjectionTestExecutionListener`提供的(请参阅 Dependency Injection of Test Fixtures)。

Dependency injection by using @Autowired is provided by the DependencyInjectionTestExecutionListener, which is configured by default (see Dependency Injection of Test Fixtures).

使用 TestContext 框架的测试类无需扩展任何特定类或实现特定接口以配置其应用程序上下文。相反,可以通过在类级别声明 “@ContextConfiguration”注释来实现配置。如果你的测试类未显式声明应用程序上下文资源位置或组件类,配置的 “ContextLoader” 将确定如何从默认位置或默认配置类加载上下文。除了上下文资源位置和组件类,应用程序上下文还可以通过应用程序上下文初始化程序进行配置。

Test classes that use the TestContext framework do not need to extend any particular class or implement a specific interface to configure their application context. Instead, configuration is achieved by declaring the @ContextConfiguration annotation at the class level. If your test class does not explicitly declare application context resource locations or component classes, the configured ContextLoader determines how to load a context from a default location or default configuration classes. In addition to context resource locations and component classes, an application context can also be configured through application context initializers.

以下部分说明如何使用 Spring 的 “@ContextConfiguration” 注释通过 XML 配置文件、Groovy 脚本、组件类(通常为 “@Configuration” 类)或上下文初始化程序配置测试 “ApplicationContext”。或者,你可以实现和配置自己的自定义 “SmartContextLoader” 以满足高级用例。

The following sections explain how to use Spring’s @ContextConfiguration annotation to configure a test ApplicationContext by using XML configuration files, Groovy scripts, component classes (typically @Configuration classes), or context initializers. Alternatively, you can implement and configure your own custom SmartContextLoader for advanced use cases.