[英]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.