【问题标题】:Using Spring @ActiveProfile in integration tests在集成测试中使用 Spring @ActiveProfile
【发布时间】:2013-08-29 12:35:11
【问题描述】:

我正在使用@Profile Spring 注释在嵌入式、独立和容器管理的数据源之间进行选择。为了选择“嵌入式”,我的集成测试被注释以激活适当的配置文件:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={TestConfigWrapper.class})
@ActiveProfiles({"EMBEDDED_DB"})
public class SomeIntegrationTest {

问题是我想将 '@ActiveProfiles' 移动到 TestConfigWrapper 中,但是这样做不会被拾取并且应用程序上下文不会加载任何数据源。

这意味着我必须使用 @ActiveProfile 注释每个集成测试,这实际上意味着它成为集成测试样板并且很容易阻碍未来的重构。

有没有办法使用 java config 做到这一点?

【问题讨论】:

  • @ContextConfiguration 和 @ActiveProfiles 如果我没记错的话可以被继承,因此虽然不灵活,但你可以将它们放在一个抽象类上并让每个集成测试扩展它。
  • 完美——这就是我最终使用的:@WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={WebAppInitializer.class}) @ActiveProfiles({Profiles.EMBEDDED_DB}) 公共抽象类 ProfiledIntegrationTest { }

标签: spring spring-annotations spring-test spring-profiles


【解决方案1】:

Hippooom 的每条评论使用抽象类来配置测试:

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={WebAppInitializer.class})
@ActiveProfiles({Profiles.EMBEDDED_DB})
public abstract class ProfiledIntegrationTest {

}

【讨论】:

  • 另一种选择是将其作为注释。然后你只需要在你的测试类上使用那个注释。两者应该有同等的影响。
猜你喜欢
  • 1970-01-01
  • 2019-03-07
  • 2021-05-17
  • 1970-01-01
  • 2015-06-14
  • 2015-01-01
  • 2020-06-06
  • 2018-02-01
  • 2020-02-10
相关资源
最近更新 更多