|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
使用 Groovy 脚本的上下文配置
加载应用上下文对于你的测试,使用使用 Groovy 脚本(使用 Groovy Bean Definition DSL)的脚本,你可以进行注释
你的测试类@ContextConfiguration并配置地点或值属性,并带有包含 Groovy 脚本资源位置的数组。资源
Groovy 脚本的查找语义与 XML 配置文件的语义相同。
|
启用 Groovy 脚本支持 支持使用 Groovy 脚本加载应用上下文春季
如果 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配置文件的位置。 |
如果你省略了两个地点和值属性来自@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脚本,方法是使用
这 以下列表展示了如何在积分测试中将两者结合:
|