|
此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 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 属性。 |
参见使用测试属性源进行上下文配置中的示例和更多详细信息。