0


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

springboot test 配置文件加载顺序及覆盖关系

参照目录结构:

目录结构

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")publicclassXxxBean{...}

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)publicclassSpringTest{...}

本文转载自: https://blog.csdn.net/hey_liar/article/details/130710351
版权归原作者 Coder_Sean 所有, 如有侵权,请联系我们删除。

“【springboot test】springboot 单元测试配置文件加载顺序及覆盖关系”的评论:

还没有评论