【问题标题】:Profiles with @RunWith(SpringJUnit4ClassRunner.class)带有@RunWith(SpringJUnit4ClassRunner.class) 的配置文件
【发布时间】:2018-01-23 15:38:12
【问题描述】:

我的 TestSuite 有两种配置,它们提供了不同的注入 bean。只要我使用注释设置我的个人资料,这就会起作用。

@ActiveProfiles( profiles={"a"})@ActiveProfiles( profiles={"b"})

但我似乎无法从属性源文件中设置它

我的注释看起来像

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AConfig.class, BConfig.class })
@PropertySource("classpath:/application.properties")
@TestPropertySource(locations = {"classpath:/application.properties"})
public abstract class AbstractTestIT {
   ...
}

application.properties的内容是

spring.profiles.active="a"

结果导致依赖不满足

如上所述,@ActiveProfiles 的设置有效,并且使用了正确的 be。

就好像 PropertySource 和/或 TestPropertySource 不适用于 @RunWith(SpringJUnit4ClassRunner.class)

【问题讨论】:

  • 尝试使用 EL 来查看您是否可以实际访问测试中的任何属性?这将确定是否正在加载属性文件

标签: java spring spring-junit springjunit4classrunner


【解决方案1】:

只要我用注释设置我的个人资料,它就可以工作。

这是意料之中的。

ActiveProfiles 不依赖于 spring.profiles.active 属性。

ActiveProfiles 是一个类级别的注解,用于声明 加载时应使用哪些活动 bean 定义配置文件 测试类的 ApplicationContext。

作为value 属性别名的profiles 属性需要与配置文件一起评估以激活测试

要激活的 bean 定义配置文件。

它不使用spring.profiles.active 属性。

spring.profiles.active 属性指定哪些配置文件在应用程序的整体配置中处于活动状态,而不是用于单元测试上下文。

【讨论】:

  • 感谢您的回答。我打算在 application.properties 文件中提供默认配置文件,然后在 CI 下运行时使用 env 覆盖配置文件。我怎样才能达到同样的结果?
猜你喜欢
  • 2021-06-22
  • 1970-01-01
  • 2017-07-05
  • 2013-01-27
  • 1970-01-01
  • 2017-04-17
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多