@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