@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 配置文件应处于活动状态。 |
以下示例表明 dev 和 integration 配置文件都应处于活动状态
-
Java
-
Kotlin
@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
class DeveloperIntegrationTests {
// class body...
}
| 1 | 表明 dev 和 integration 配置文件都应处于活动状态。 |
@ContextConfiguration
@ActiveProfiles(["dev", "integration"]) (1)
class DeveloperIntegrationTests {
// class body...
}
| 1 | 表明 dev 和 integration 配置文件都应处于活动状态。 |
@ActiveProfiles 默认支持继承超类和封闭类声明的活动 bean 定义配置文件。您还可以通过实现自定义的 ActiveProfilesResolver 并使用 @ActiveProfiles 的 resolver 属性注册它来以编程方式解析活动 bean 定义配置文件。 |
有关示例和更多详细信息,请参阅使用环境配置文件的上下文配置、@Nested 测试类配置以及 @ActiveProfiles javadoc。