Context Configuration with Groovy Scripts

使用基于Groovy Bean Definition DSL的Groovy脚本载入`ApplicationContext`测试时,可以通过`@ContextConfiguration`注解测试类并在`locations`或`value`属性中配置一个包含Groovy脚本资源位置的数组。Groovy脚本的资源查找语义与XML configuration files所述的相同。

To load an ApplicationContext for your tests by using Groovy scripts that use the Groovy Bean Definition DSL, you can annotate your test class with @ContextConfiguration and configure the locations or value attribute with an array that contains the resource locations of Groovy scripts. Resource lookup semantics for Groovy scripts are the same as those described for XML configuration files.

Enabling Groovy script support

如果 Groovy 在类加载路径上,则自动启用使用 Groovy 脚本加载 ApplicationContext 的支持。

Enabling Groovy script support

Support for using Groovy scripts to load an ApplicationContext in the Spring TestContext Framework is enabled automatically if Groovy is on the classpath.

以下示例显示如何指定 Groovy 配置文件:

The following example shows how to specify Groovy configuration files:

Java
@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from "/AppConfig.groovy" and
// "/TestConfig.groovy" in the root of the classpath
@ContextConfiguration({"/AppConfig.groovy", "/TestConfig.Groovy"}) 1
class MyTest {
	// class body...
}
1 Specifying the location of Groovy configuration files.
Kotlin
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from "/AppConfig.groovy" and
// "/TestConfig.groovy" in the root of the classpath
@ContextConfiguration("/AppConfig.groovy", "/TestConfig.Groovy") (1)
class MyTest {
	// class body...
}
2 Specifying the location of Groovy configuration files.

如果您从 @ContextConfiguration 标注中省略 locationsvalue 属性,TestContext 框架将尝试检测一个默认 Groovy 脚本。具体来说,GenericGroovyXmlContextLoaderGenericGroovyXmlWebContextLoader 根据测试类的名称检测一个默认位置。如果您的类被命名为 com.example.MyTest,则 Groovy 上下文加载器将从 "classpath:com/example/MyTestContext.groovy" 加载您的应用程序上下文。以下示例显示如何使用默认值:

If you omit both the locations and value attributes from the @ContextConfiguration annotation, the TestContext framework tries to detect a default Groovy script. Specifically, GenericGroovyXmlContextLoader and GenericGroovyXmlWebContextLoader detect a default location based on the name of the test class. If your class is named com.example.MyTest, the Groovy context loader loads your application context from "classpath:com/example/MyTestContext.groovy". The following example shows how to use the default:

Java
@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTestContext.groovy"
@ContextConfiguration (1)
class MyTest {
	// class body...
}
1 Loading configuration from the default location.
Kotlin
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTestContext.groovy"
@ContextConfiguration (1)
class MyTest {
	// class body...
}
2 Loading configuration from the default location.
Declaring XML configuration and Groovy scripts simultaneously

您可以通过使用 @ContextConfigurationlocationsvalue 属性同时声明 XML 配置文件和 Groovy 脚本。如果已配置资源位置的路径以 .xml 结尾,则使用 XmlBeanDefinitionReader 加载它。否则,使用 GroovyBeanDefinitionReader 加载它。

You can declare both XML configuration files and Groovy scripts simultaneously by using the locations or value attribute of @ContextConfiguration. If the path to a configured resource location ends with .xml, it is loaded by using an XmlBeanDefinitionReader. Otherwise, it is loaded by using a GroovyBeanDefinitionReader.

以下清单展示如何在集成测试中结合二者:

The following listing shows how to combine both in an integration test:

Java
@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from
// "/app-config.xml" and "/TestConfig.groovy"
@ContextConfiguration({ "/app-config.xml", "/TestConfig.groovy" })
class MyTest {
	// class body...
}
Kotlin
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from
// "/app-config.xml" and "/TestConfig.groovy"
@ContextConfiguration("/app-config.xml", "/TestConfig.groovy")
class MyTest {
	// class body...
}