为了账号安全,请及时绑定邮箱和手机立即绑定

在测试中的 Spring Environment 中使用带有 spring.profiles

在测试中的 Spring Environment 中使用带有 spring.profiles

幕布斯7119047 2022-10-07 16:36:36
根据文档(https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--)你可以使用@EnabledIf测试类或测试方法上的注释,如下所示:@EnabledIf("${smoke.tests.enabled}")或者@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")其中字符串是 Spring 环境中可用属性的占位符。假设我有以下application.yml文件:smoke:  tests:    enabled: truespring:  profiles:    active: test以及以下测试类:@EnabledIf(value = "#{${spring.profiles.active} == 'test'}", loadContext = true)@SpringBootTestpublic class SomeClassForTests {    @Autowired SomeType autowiredType;    @Test    public void someTest() {        // test logic...    }
查看完整描述

3 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

要检查 Spring 中属性的确切值Environment,您应该使用以下方法。

@EnabledIf(expression = "#{environment['spring.profiles.active'] == 'test'}", loadContext = true)

要检查 Spring 中哪些配置文件当前处于活动状态Environment,您应该使用以下方法。

@EnabledIf(expression = "#{environment.acceptsProfiles('test', 'someotherprofile')}", loadContext = true)



查看完整回答
反对 回复 2022-10-07
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

在 SpEL 中使用时,请尝试使用额外的撇号来包装属性:

@EnabledIf(value = "#{'${spring.profiles.active}' == 'test'}", loadContext = true)

当我尝试使用自定义属性时,我为我工作(终于!)


查看完整回答
反对 回复 2022-10-07
?
慕神8447489

TA贡献1780条经验 获得超1个赞

您的表达式应如下所示:

@EnabledIf(value = "${spring.profiles.active == 'test'}", loadContext = true)



查看完整回答
反对 回复 2022-10-07
  • 3 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信