@TestPropertySource

@TestPropertySource 是一种可以应用于测试类以配置属性文件的路径和要添加到集成测试的 ApplicationContextEnvironment 中的 PropertySources 集合中的内联属性的注释。

@TestPropertySource is an annotation that can be applied to a test class to configure the locations of properties files and inlined properties to be added to the set of PropertySources in the Environment for an ApplicationContext loaded for an integration test.

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

The following example demonstrates how to declare a properties file from the classpath:

Java
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
1 Get properties from test.properties in the root of the classpath.
Kotlin
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
2 Get properties from test.properties in the root of the classpath.

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

The following example demonstrates how to declare inlined properties:

Java
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) (1)
class MyIntegrationTests {
	// class body...
}
1 Declare timezone and port properties.
Kotlin
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) (1)
class MyIntegrationTests {
	// class body...
}
2 Declare timezone and port properties.

有关示例和进一步的详细信息,请参见 Context Configuration with Test Property Sources

See Context Configuration with Test Property Sources for examples and further details.