@TestPropertySource

@TestPropertySource 是一個註解,可應用於測試類,用於配置屬性檔案的位置和內聯屬性,這些屬性將新增到為整合測試載入的 ApplicationContextEnvironment 中的 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 宣告 timezoneport 屬性。
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) (1)
class MyIntegrationTests {
	// class body...
}
1 宣告 timezoneport 屬性。

有關示例和更多詳細資訊,請參閱 使用測試屬性源進行上下文配置

© . This site is unofficial and not affiliated with VMware.