@ActiveProfiles

@ActiveProfiles 是一個可以應用於測試類的註解,用於宣告在為整合測試載入 ApplicationContext 時應啟用哪些 bean 定義配置檔案。

以下示例表明 dev 配置檔案應處於活動狀態

  • Java

  • Kotlin

@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
1 表明 dev 配置檔案應處於活動狀態。
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
1 表明 dev 配置檔案應處於活動狀態。

以下示例表明 devintegration 配置檔案都應處於活動狀態

  • Java

  • Kotlin

@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
class DeveloperIntegrationTests {
	// class body...
}
1 表明 devintegration 配置檔案都應處於活動狀態。
@ContextConfiguration
@ActiveProfiles(["dev", "integration"]) (1)
class DeveloperIntegrationTests {
	// class body...
}
1 表明 devintegration 配置檔案都應處於活動狀態。
@ActiveProfiles 預設支援繼承超類和封閉類宣告的活動 bean 定義配置檔案。您還可以透過實現自定義的 ActiveProfilesResolver 並使用 @ActiveProfilesresolver 屬性註冊它來以程式設計方式解析活動 bean 定義配置檔案。

有關示例和更多詳細資訊,請參閱使用環境配置檔案的上下文配置@Nested 測試類配置以及 @ActiveProfiles javadoc。

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