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

@TestPropertySource

@TestPropertySource是类级注解,可用于配置 要添加到PropertySourcesEnvironment对于ApplicationContextloaded 为 集成测试。spring-doc.cadn.net.cn

下面的示例演示了如何从 Classpath 声明属性文件:spring-doc.cadn.net.cn

@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
1 从以下位置获取属性test.properties在 Classpath 的根目录中。
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
1 从以下位置获取属性test.properties在 Classpath 的根目录中。

下面的示例演示如何声明内联属性:spring-doc.cadn.net.cn

@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) (1)
class MyIntegrationTests {
	// class body...
}
1 timezoneport性能。
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) (1)
class MyIntegrationTests {
	// class body...
}
1 timezoneport性能。