@TestPropertySource

@TestPropertySource 是一个注解,可以应用于测试类,用于配置属性文件的位置和内联属性,这些属性将添加到为集成测试加载的 ApplicationContextEnvironment 中的 PropertySources 集合中。

以下示例演示如何从类路径声明属性文件:

Java
@ContextConfiguration
@TestPropertySource("/test.properties") [id="CO1-1"][id="CO1-1"][id="CO1-1"](1)
class MyIntegrationTests {
	// class body...
}
<1>  从类路径根目录的 `test.properties` 获取属性。
Kotlin
@ContextConfiguration
@TestPropertySource("/test.properties") [id="CO2-1"][id="CO1-2"][id="CO2-1"](1)
class MyIntegrationTests {
	// class body...
}
<1>  从类路径根目录的 `test.properties` 获取属性。

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

Java
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) [id="CO3-1"][id="CO1-3"][id="CO3-1"](1)
class MyIntegrationTests {
	// class body...
}
<1>  声明 `timezone` 和 `port` 属性。
Kotlin
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) [id="CO4-1"][id="CO1-4"][id="CO4-1"](1)
class MyIntegrationTests {
	// class body...
}
<1>  声明 `timezone` 和 `port` 属性。

有关示例和更多详细信息,请参阅 使用测试属性源进行上下文配置