@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中的示例和更多详细信息。