|
此版本仍在开发中,尚未稳定。如需最新的稳定版本,请使用 Spring Framework 7.0.6! |
@TestPropertySource
@TestPropertySource 是一个可用于测试类的注解,用于配置属性文件的位置以及要添加到
PropertySources 集合中的内联属性,这些属性是为了加载集成测试中的
Environment 的 ApplicationContext 而设置的。
以下示例展示了如何从类路径声明属性文件:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
// class body...
}
| 1 | 从类路径的根目录中的test.properties获取属性。 |
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
// class body...
}
| 1 | 从类路径的根目录中的test.properties获取属性。 |
以下示例演示如何声明内联属性:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) (1)
class MyIntegrationTests {
// class body...
}
| 1 | 声明 timezone 和 port 属性。 |
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) (1)
class MyIntegrationTests {
// class body...
}
| 1 | 声明 timezone 和 port 属性。 |
示例和更多详情请参见测试属性源的上下文配置。