Context Configuration with XML resources
要使用 XML 配置文件加载测试的 ApplicationContext,可以使用 @ContextConfiguration 注解测试类。locations 属性指定 XML 配置元数据文件的位置。可以将 locations 属性配置为包含文件路径数组,其中路径可以是绝对路径、相对路径或资源 URL。默认情况下,从 "classpath:{测试类名称}-context.xml" 加载 ApplicationContext。
要通过使用 XML 配置文件加载测试的 ApplicationContext
,请用 @ContextConfiguration
注释你的测试类,并将 locations
属性配置为一个包含 XML 配置元数据资源位置的数组。一个普通路径或相对路径(例如 context.xml
)被视为相对于定义测试类的包的类路径资源。以斜杠开头的路径被视为一个绝对的类路径位置(例如 ` /org/example/config.xml`)。一个表示资源 URL 的路径(即以 classpath:
, file:
, http:
, 等为前缀的路径)被 “照原样” 使用。
To load an ApplicationContext
for your tests by using XML configuration files, annotate
your test class with @ContextConfiguration
and configure the locations
attribute with
an array that contains the resource locations of XML configuration metadata. A plain or
relative path (for example, context.xml
) is treated as a classpath resource that is
relative to the package in which the test class is defined. A path starting with a slash
is treated as an absolute classpath location (for example, /org/example/config.xml
). A
path that represents a resource URL (i.e., a path prefixed with classpath:
, file:
,
http:
, etc.) is used as is.
- Java
-
@ExtendWith(SpringExtension.class) // ApplicationContext will be loaded from "/app-config.xml" and // "/test-config.xml" in the root of the classpath @ContextConfiguration(locations = {"/app-config.xml", "/test-config.xml"}) (1) class MyTest { // class body... }
1 | Setting the locations attribute to a list of XML files.
|
2 | Setting the locations attribute to a list of XML files. |
@ContextConfiguration
通过标准 Java value
属性支持 locations
属性的一个别名。因此,如果你不需要在 @ContextConfiguration
中声明其他属性,则可以省略 locations
属性名称的声明,并使用以下示例中演示的简写格式声明资源位置:
@ContextConfiguration
supports an alias for the locations
attribute through the
standard Java value
attribute. Thus, if you do not need to declare additional
attributes in @ContextConfiguration
, you can omit the declaration of the locations
attribute name and declare the resource locations by using the shorthand format
demonstrated in the following example:
- Java
-
@ExtendWith(SpringExtension.class) @ContextConfiguration({"/app-config.xml", "/test-config.xml"}) 1 class MyTest { // class body... }
1 | Specifying XML files without using the locations attribute.
|
2 | Specifying XML files without using the locations attribute. |
如果你从 @ContextConfiguration
注释中省略了 locations
和 value
属性,TestContext 框架将尝试检测一个默认 XML 资源位置。具体来说,GenericXmlContextLoader
和 GenericXmlWebContextLoader
根据测试类的名称检测一个默认位置。如果你的类名为 com.example.MyTest
,GenericXmlContextLoader
将从 "classpath:com/example/MyTest-context.xml"
加载你的应用程序上下文。以下示例展示了如何这样做:
If you omit both the locations
and the value
attributes from the
@ContextConfiguration
annotation, the TestContext framework tries to detect a default
XML resource location. Specifically, GenericXmlContextLoader
and
GenericXmlWebContextLoader
detect a default location based on the name of the test
class. If your class is named com.example.MyTest
, GenericXmlContextLoader
loads your
application context from "classpath:com/example/MyTest-context.xml"
. The following
example shows how to do so:
- Java
-
@ExtendWith(SpringExtension.class) // ApplicationContext will be loaded from // "classpath:com/example/MyTest-context.xml" @ContextConfiguration (1) class MyTest { // class body... }
1 | Loading configuration from the default location.
|
2 | Loading configuration from the default location. |