简体   繁体   English

Spring Boot JUnit和@TestPropertySource使用多个属性文件

[英]Spring Boot JUnit and @TestPropertySource using multiple property files

Spring Boot 2.0.3.RELEASE Spring Boot 2.0.3.RELEASE

Have more than one properties file to deal with .. the application.properties and application-DEV.properties 有多个属性文件可以处理.. application.properties和application-DEV.properties

using the annotation @TestPropertySource in a Junit test I can only get it to read one file: 在Junit测试中使用注释@TestPropertySource我只能让它读取一个文件:

@TestPropertySource("file:C:\\Users\\user\\eclipse-workspace\\one2one_httpCall\\src\\main\\resources\\application-DEV.properties")

works as expected 按预期工作

However I need two properties file I did see the locations option but did not see an example of more than one file .. tried different options but none worked: 但是,我需要两个属性文件,我确实看到位置选项,但没有看到多个文件的示例..尝试了不同的选项,但没有工作:

@TestPropertySource(locations = "classpath:application-DEV.properties;classpath:application.properties")

Tried a couple of ways I am not posting and even tried using @TestPropertySource twice but error saying u cannot use it twice. 尝试了几种我没有张贴的方法,甚至尝试过使用@TestPropertySource两次,但是错误说你不能两次使用它。

tried using @PropertySource since u can use it twice but did not work since this is a Junit test. 尝试使用@PropertySource因为你可以使用它两次但是没有用,因为这是一个Junit测试。 Looked at a bunch of questions on stacktrace + others and tried but no luck. 看了一堆关于stacktrace +其他的问题并试过但没有运气。

So my question is how to use two properties files via the @TestPropertySource annotation? 所以我的问题是如何通过@TestPropertySource注释使用两个属性文件?

If you look inside the definition of @TestPropertySource , you will see that locations is of type String [] . 如果查看@TestPropertySource的定义,您将看到locations的类型为String [] Therefore, if you need to pass it multiple values, you must do so with an array: 因此,如果需要传递多个值,则必须使用数组:

@TestPropertySource(locations = { "classpath:application.properties", "classpath:application-DEV.properties" })

Also, pay attention to the order in which you declare your properties files. 另外,请注意声明属性文件的顺序。 As stated in the TestPropertySource docs : TestPropertySource文档中所述

Each location will be added to the enclosing Environment as its own property source, in the order declared . 每个位置将按声明的顺序添加到封闭环境中作为其自己的属性源。

So you would probably want to declare your DEV properties after to avoid them being overriden by your production properties. 因此,您可能希望在之后声明DEV属性以避免它们被生产属性覆盖。

暂无
暂无

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

相关问题 在Spring Boot中使用@TestPropertySource覆盖@PropertySource - Override @PropertySource with @TestPropertySource in Spring Boot 使用@TestPropertySource (Spring Boot 2) 正确定义测试属性的路径 - Correctly define path to test properties using @TestPropertySource (Spring Boot 2) 无法使用 Spring Boot、JUnit5 和 Kotlin 加载带有 @TestPropertySource 的 test-properties.yaml 文件 - Can't load a test-properties.yaml file with the @TestPropertySource with Spring Boot, JUnit5 and Kotlin Spring Boot 5多个JUnit JPA测试文件接线 - Spring Boot 5 multiple JUnit JPA test files wiring Spring Boot 测试:@TestPropertySource 不覆盖 @EnableAutoConfiguration - Spring Boot Test: @TestPropertySource not overriding @EnableAutoConfiguration Spring Boot:@TestPropertySource未从导入的配置加载 - Spring Boot: @TestPropertySource not loaded from imported configuration 两个Spring JUnit测试类:@TestPropertySource值不同时的InstanceAlreadyExistsException - Two Spring JUnit test classes: InstanceAlreadyExistsException when @TestPropertySource value differs @TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试 - @TestPropertySource doesn't work for JUnit test with AnnotationConfigContextLoader in Spring 1.2.6 对多个测试类使用一个 @TestPropertySource - Using one @TestPropertySource for multiple test classes 基于请求的同一 Spring Boot 应用程序的多个属性文件 - Multiple property files for same Spring Boot Application based on request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM