在Spring测试的上下文中@IfProfileValue vs @ActiveProfiles

bal*_*teo 7 spring-test

我参考了Spring测试和以下注释:

我目前@ActiveProfiles在我的应用程序中使用,我最近发现了@IfProfileValue注释的存在,似乎提供了类似的功能.

有人可以解释这两个注释之间的差异,或许通过提供可以对比两者的用法示例吗?

Sam*_*nen 21

如Javadoc中所述,@IfProfileValue用于指示针对特定测试配置文件或环境启用了测试.

然而,@ActiveProfiles用于声明在为测试类加载ApplicationContext时应使用哪些活动bean定义概要文件.

换句话说,您使用@IfProfileValue控制测试类或测试方法是否会被执行或者跳过,并且使用@ActiveProfiles设置将被用于激活bean定义配置文件加载ApplicationContext您的测试.

请注意@IfProfileValue,bean定义概要的概念之前就已在Spring Framework 2.0中@ActiveProfiles引入,并且最初是在Spring Framework 3.1中引入的.

两个注释都包含术语配置文件,但实际上它们完全不相关!

在考虑语义时,术语配置文件可能会产生误导@IfProfileValue.关键是考虑测试组(如TestNG中的测试组)而不是配置文件.请参阅JavaDoc中的示例以获取@IfProfileValue.这是一段摘录:

@IfProfileValue(name = "test-groups", values = { "unit-tests", "integration-tests" })
public void testWhichRunsForUnitOrIntegrationTestGroups() {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

如果设置test-groups系统属性(例如,-Dtest-groups=unit-tests-Dtest-groups=integration-tests),将执行上述测试方法.

" Spring参考手册" 中" 测试"一章的" 环境配置文件上下文配置"部分提供了有关如何使用的详细示例.@ActiveProfiles

问候,

Sam(Spring TestContext Framework的作者)