@ActiveProfiles

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

以下示例表示 dev profile 應該被啟用

  • Java

  • Kotlin

@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
1 表示 dev profile 應該被啟用。
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
1 表示 dev profile 應該被啟用。

以下示例表示 devintegration 兩個 profile 都應該被啟用

  • Java

  • Kotlin

@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
class DeveloperIntegrationTests {
	// class body...
}
1 表示 devintegration profile 都應該被啟用。
@ContextConfiguration
@ActiveProfiles(["dev", "integration"]) (1)
class DeveloperIntegrationTests {
	// class body...
}
1 表示 devintegration profile 都應該被啟用。
預設情況下,@ActiveProfiles 支援繼承由超類和封閉類宣告的活動 bean 定義 profile。您還可以透過實現自定義的 ActiveProfilesResolver 並使用 @ActiveProfilesresolver 屬性註冊它來以程式設計方式解析活動 bean 定義 profile。

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