@ActiveProfiles

@ActiveProfiles 是可以应用于测试类以声明当加载用于集成测试的 ApplicationContext 时应激活哪些 bean 定义配置文件的注解。

@ActiveProfiles is an annotation that can be applied to a test class to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test.

以下示例表明应激活 dev 配置文件:

The following example indicates that the dev profile should be active:

Java
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
1 Indicate that the dev profile should be active.
Kotlin
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
	// class body...
}
2 Indicate that the dev profile should be active.

以下示例表明应激活 devintegration 配置文件:

The following example indicates that both the dev and the integration profiles should be active:

Java
@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
class DeveloperIntegrationTests {
	// class body...
}
1 Indicate that the dev and integration profiles should be active.
Kotlin
@ContextConfiguration
@ActiveProfiles(["dev", "integration"]) (1)
class DeveloperIntegrationTests {
	// class body...
}
2 Indicate that the dev and integration profiles should be active.

@ActiveProfiles 默认支持从超类和封闭类继承活动 bean 定义配置文件。你还可以通过实现自定义 ActiveProfilesResolver 并使用 @ActiveProfilesresolver 属性注册 ActiveProfilesResolver 来以编程方式解析活动 bean 定义配置文件。

@ActiveProfiles provides support for inheriting active bean definition profiles declared by superclasses and enclosing classes by default. You can also resolve active bean definition profiles programmatically by implementing a custom ActiveProfilesResolver and registering it by using the resolver attribute of @ActiveProfiles.