|
对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
使用 Groovy 脚本进行上下文配置
要加载ApplicationContext对于使用使用 Groovy Bean 定义 DSL 的 Groovy 脚本进行测试,您可以对
您的测试类使用@ContextConfiguration并配置locations或value属性,其中包含 Groovy 脚本的资源位置的数组。资源
Groovy 脚本的查找语义与 XML 配置文件描述的语义相同。
|
启用 Groovy 脚本支持 支持使用 Groovy 脚本加载ApplicationContext在Spring
如果 Groovy 在类路径上,则会自动启用 TestContext 框架。 |
以下示例显示了如何指定 Groovy 配置文件:
-
Java
-
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...
}
| 1 | 指定 Groovy 配置文件的位置。 |
@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 | 指定 Groovy 配置文件的位置。 |
如果同时省略locations和value属性@ContextConfiguration注释时,TestContext 框架会尝试检测默认的 Groovy 脚本。
具体说来GenericGroovyXmlContextLoader和GenericGroovyXmlWebContextLoader根据测试类的名称检测默认位置。如果您的类名为com.example.MyTest,Groovy 上下文加载器从"classpath:com/example/MyTestContext.groovy".以下示例演示如何使用
默认值:
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTestContext.groovy"
@ContextConfiguration (1)
class MyTest {
// class body...
}
| 1 | 从默认位置加载配置。 |
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTestContext.groovy"
@ContextConfiguration (1)
class MyTest {
// class body...
}
| 1 | 从默认位置加载配置。 |
|
同时声明 XML 配置和 Groovy 脚本
您可以使用以下命令同时声明 XML 配置文件和 Groovy 脚本
这 以下列表显示了如何在集成测试中组合两者:
|