@IfProfileValue vs @ActiveProfiles in the context of Spring test

最后都变了- 提交于 2019-12-01 04:44:26

As stated in the Javadoc, @IfProfileValue is used to indicate that a test is enabled for a specific testing profile or environment.

Whereas, @ActiveProfiles is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.

In other words, you use @IfProfileValue to control whether a test class or test method will be executed or skipped, and you use @ActiveProfiles to set the active bean definition profiles that will be used to load the ApplicationContext for your test.

Please note that @IfProfileValue was introduced in Spring Framework 2.0, long before the notion of bean definition profiles, and @ActiveProfiles was first introduced in Spring Framework 3.1.

Both annotations contain the term profile, but they are actually completely unrelated!

The term profile is perhaps misleading when considering the semantics for @IfProfileValue. The key is to think about test groups (like those in TestNG) instead of profiles. See the examples in the JavaDoc for @IfProfileValue. Here's an excerpt:

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

The above test method would be executed if you set the test-groups system property (e.g., -Dtest-groups=unit-tests or -Dtest-groups=integration-tests).

The Context configuration with environment profiles section of the Testing chapter in the Spring Reference manual provides detailed examples of how to use @ActiveProfiles.

Regards,

Sam (author of the Spring TestContext Framework)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!