@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。