@TestPropertySource

@TestPropertySource 是一个注解,可应用于测试类,用于配置属性文件的位置以及要添加到集成测试加载的 ApplicationContext 环境中的 PropertySources 集合的内联属性。

以下示例演示如何从 classpath 声明属性文件

  • Java

  • Kotlin

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

以下示例演示如何声明内联属性

  • Java

  • Kotlin

@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 属性。

有关示例和更多详细信息,请参阅Context Configuration with Test Property Sources