【springboot test】springboot 单元测试配置文件加载顺序及覆盖关系

参照目录结构:

目录结构

1.配置文件加载基础原则:

通过任意方式指定的application-xxx.yml中会覆盖application.yml中同名配置,application.yml一般作为兜底或通用配置

2.application.yml主配置文件加载原则:

[ 实际运行的application.yml ] = [ test/resources/application.yml ] ? [ test/resources/application.yml ] : [ main/resources/application.yml ]

3.application.yml中指定spring.profiles.active:xxx时,xxx的加载原则:

[ 实际运行的application-xxx.yml ] = [ test/resources/application-xxx.yml ] ? [ test/resources/application-xxx.yml ] : [ main/resources/application-xxx.yml ]

4.使用@ActiveProfiles(“yyy”)时:

  • yyy会覆盖 application.yml中指定spring.profiles.active:xxx 指定的xxx, xxx不生效
  • application-yyy.yml加载原则同 原则3
  • yyy可与xxx一致, 原则同理

5.其他自定义配置文件,如xxx.properties:

5.1

  • 一般自定义xxx.properties有对应的xxxBean, 会用@PropertySource指定xxx.properties
  • 此时[ 实际运行的alipay.properties ] = [ test/resources/alipay.properties ] ? [ test/resources/alipay.properties ] : [ main/resources/alipay.properties ]
  • test 和 main 不会同时加载
@Data
@Component
@PropertySource(value = {"classpath:/xxx.properties"})
@ConfigurationProperties(prefix = "xxx")
public class XxxBean {...}

5.2@PropertySource(“classpath:/xxx.properties”)同时使用@TestPropertySource(“classpath:yyy.properties”)时:

  • yyy.properties和xxx.properties同时加载
  • yyy.properties优先覆盖xxx.properties同名内容
  • xxx/yyy.properties 的 main/test位置读取原则同上
@ActiveProfiles("sb")
@TestPropertySource({"classpath:yyy.properties"})
@SpringBootTest(classes = AlipayApplication.class)
@RunWith(SpringRunner.class)
public class SpringTest {...}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值