@TestPropertySource
@TestPropertySource
是一個可以應用於測試類別的註解,用於組態屬性檔案的位置和內嵌屬性,以新增至為整合測試載入的 ApplicationContext
的 Environment
中的 PropertySources
集合。
以下範例示範如何從類別路徑宣告屬性檔案
-
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 屬性。 |
請參閱 使用測試屬性來源進行 Context 組態 以取得範例和更多詳細資訊。