对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

使用 Groovy 脚本的上下文配置

加载应用上下文对于你的测试,使用使用 Groovy 脚本(使用 Groovy Bean Definition DSL)的脚本,你可以进行注释 你的测试类@ContextConfiguration并配置地点属性,并带有包含 Groovy 脚本资源位置的数组。资源 Groovy 脚本的查找语义与 XML 配置文件的语义相同。spring-doc.cadn.net.cn

启用 Groovy 脚本支持
支持使用 Groovy 脚本加载应用上下文春季 如果 Groovy 在类路径上,TestContext 框架会自动启用。

以下示例展示了如何指定 Groovy 配置文件:spring-doc.cadn.net.cn

@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脚本。 具体说来GenericGroovyXmlContextLoaderGenericGroovyXmlWebContextLoader根据测试类别名称检测默认位置。如果你的职业被命名com.example.MyTest,Groovy 上下文加载器从 加载你的应用上下文“classpath:com/example/MyTestContext.groovy”.以下示例展示了如何使用 默认情况:spring-doc.cadn.net.cn

@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脚本,方法是使用 这地点属性@ContextConfiguration.如果路径是 配置好的资源位置以.xml,通过使用XmlBeanDefinitionReader.否则,通过使用GroovyBean定义阅读器.spring-doc.cadn.net.cn

以下列表展示了如何在积分测试中将两者结合:spring-doc.cadn.net.cn

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