简体   繁体   English

Spring 引导配置文件属性不适用于测试

[英]Spring Boot profile properties doesn't work with tests

Spring Boot version 2.2.6. Spring 引导版本 2.2.6。 I have case that there is several application-{profile}.yml files in folder src/main/resources/ and I want't to build project with Maven eg mvn clean package -Dspring.profiles.active=dev我的情况是文件夹src/main/resources/中有几个application-{profile}.yml文件,我不想使用 Maven 构建项目,例如mvn clean package -Dspring.profiles.active=dev

Then I have just application.yml file in folder src/test/resources , this should be a properties file to all test (IT/unit).然后我在文件夹src/test/resources中只有application.yml文件,这应该是所有测试(IT/单元)的属性文件。

Now when I build with command mvn clean package -Dspring.profiles.active=dev , properties src/main/resources/application-dev.yml and src/test/resources/application.yml are MERGED and used for the tests.现在,当我使用命令mvn clean package -Dspring.profiles.active=dev构建时,属性src/main/resources/application-dev.ymlsrc/test/resources/application.yml被合并并用于测试。 Well in test properties there might be pretty fatal confs eg Hibernate auto-ddl: create-drop.那么在测试属性中可能会有非常致命的配置,例如 Hibernate auto-ddl: create-drop。

Have been reading docs but I don't find any logic why properties files are MERGED in this case.一直在阅读文档,但我没有找到任何逻辑为什么在这种情况下属性文件被合并。 Is there any good practice that tests can be forced to use ALWAYS certain properties file?是否有任何好的做法可以强制测试始终使用某些属性文件? I think that just using some annotations in test files isn't the best practice, eg @TestPropertySource or @ActiveProfiles , cause when you forgot to use these annotations then the case is same again.我认为仅在测试文件中使用一些注释并不是最佳实践,例如@TestPropertySource@ActiveProfiles ,因为当您忘记使用这些注释时,情况再次相同。 Is there some global configuration for all tests or some other better solutions?所有测试是否有一些全局配置或其他更好的解决方案?

The application.yml file has higher precedence over any environment-specific file, for example, application-dev.yml file. application.yml文件的优先级高于任何特定于环境的文件,例如application-dev.yml文件。 The standard inheritance applies, so you can override the parent properties in the profile-specific YAML file.标准 inheritance 适用,因此您可以覆盖特定配置文件 YAML 文件中的父属性。

application.yml

server:
  port: 9090

spring:
  jpa:
    hibernate:
      ddl-auto: create-drop

application-dev.yml

spring:
  jpa:
    hibernate:
      ddl-auto: none

In case you run with the dev profile, the value of spring.jpa.hibernate.ddl-auto parameter would be none .如果您使用dev配置文件运行,则spring.jpa.hibernate.ddl-auto参数的值为none

I get that application.yml has higher precedence in folder /src/main, but that it takes over also from folder /src/test is kinda weird.我知道 application.yml 在文件夹 /src/main 中具有更高的优先级,但它也从文件夹 /src/test 中接管有点奇怪。

Yes that config can be made into profile-specific files (ddl-auto: none), tough I still have problem with datasource, somehow Spring Boot is picking application-dev.ymls datasource into tests also, this is working like other way around than that ddl-auto attribute.是的,可以将配置制作成特定于配置文件的文件(ddl-auto:无),很难,我仍然对数据源有问题,不知何故 Spring 引导也在选择 application-dev.ymls 数据源进行测试,这与其他方式不同那个 ddl-auto 属性。

src/test/resources/application.yml

spring:
  datasource:
    url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL

src/main/resources/application-dev.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM