@ContextConfiguration

@ContextConfiguration 是一种注解,可以应用于测试类,以配置用于确定如何加载和配置集成测试的 ApplicationContext 的元数据。具体来说,@ContextConfiguration 声明用于加载上下文的应用程序上下文资源 locations 或组件 classes

@ContextConfiguration is an annotation that can be applied to a test class to configure metadata that is used to determine how to load and configure an ApplicationContext for integration tests. Specifically, @ContextConfiguration declares the application context resource locations or the component classes used to load the context.

资源位置通常是位于类路径中的 XML 配置文件或 Groovy 脚本,而组件类通常是 @Configuration 类。但是,资源位置还可以引用文件系统中的文件和脚本,而组件类可以是 @Component 类、@Service 类,等等。有关进一步的详细信息,请参见 Component Classes

Resource locations are typically XML configuration files or Groovy scripts located in the classpath, while component classes are typically @Configuration classes. However, resource locations can also refer to files and scripts in the file system, and component classes can be @Component classes, @Service classes, and so on. See Component Classes for further details.

以下示例显示一个引用 XML 文件的 @ContextConfiguration 注解:

The following example shows a @ContextConfiguration annotation that refers to an XML file:

Java
@ContextConfiguration("/test-config.xml") (1)
class XmlApplicationContextTests {
	// class body...
}
1 Referring to an XML file.
Kotlin
@ContextConfiguration("/test-config.xml") (1)
class XmlApplicationContextTests {
	// class body...
}
2 Referring to an XML file.

以下示例显示一个引用类的 @ContextConfiguration 注解:

The following example shows a @ContextConfiguration annotation that refers to a class:

Java
@ContextConfiguration(classes = TestConfig.class) (1)
class ConfigClassApplicationContextTests {
	// class body...
}
1 Referring to a class.
Kotlin
@ContextConfiguration(classes = [TestConfig::class]) (1)
class ConfigClassApplicationContextTests {
	// class body...
}
2 Referring to a class.

作为声明资源位置或组件类的替代方案或补充方案,您可以使用 @ContextConfiguration 声明 ApplicationContextInitializer 类。以下示例演示了这种情况:

As an alternative or in addition to declaring resource locations or component classes, you can use @ContextConfiguration to declare ApplicationContextInitializer classes. The following example shows such a case:

Java
@ContextConfiguration(initializers = CustomContextInitializer.class) (1)
class ContextInitializerTests {
	// class body...
}
1 Declaring an initializer class.
Kotlin
@ContextConfiguration(initializers = [CustomContextInitializer::class]) (1)
class ContextInitializerTests {
	// class body...
}
2 Declaring an initializer class.

您还可以选择使用 @ContextConfiguration 声明 ContextLoader 策略。但请注意,通常不需要显式配置加载器,因为默认加载器支持 initializers 和资源 locations 或组件 classes

You can optionally use @ContextConfiguration to declare the ContextLoader strategy as well. Note, however, that you typically do not need to explicitly configure the loader, since the default loader supports initializers and either resource locations or component classes.

以下示例同时使用位置和加载器:

The following example uses both a location and a loader:

Java
@ContextConfiguration(locations = "/test-context.xml", loader = CustomContextLoader.class) (1)
class CustomLoaderXmlApplicationContextTests {
	// class body...
}
1 Configuring both a location and a custom loader.
Kotlin
@ContextConfiguration("/test-context.xml", loader = CustomContextLoader::class) (1)
class CustomLoaderXmlApplicationContextTests {
	// class body...
}
2 Configuring both a location and a custom loader.

@ContextConfiguration 提供支持以继承资源位置或配置类,以及超类或封闭类声明的上下文初始化程序。

@ContextConfiguration provides support for inheriting resource locations or configuration classes as well as context initializers that are declared by superclasses or enclosing classes.

有关详细信息,请参阅 Context Management@Nested test class configuration和 `@ContextConfiguration`javadoc。

See Context Management, @Nested test class configuration, and the @ContextConfiguration javadocs for further details.