Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JUnit and @TestPropertySource using multiple property files

Spring Boot 2.0.3.RELEASE

Have more than one properties file to deal with .. the application.properties and application-DEV.properties

using the annotation @TestPropertySource in a Junit test I can only get it to read one file:

@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.

tried using @PropertySource since u can use it twice but did not work since this is a Junit test. Looked at a bunch of questions on stacktrace + others and tried but no luck.

So my question is how to use two properties files via the @TestPropertySource annotation?

like image 357
Oxnard Avatar asked Oct 15 '18 20:10

Oxnard


1 Answers

If you look inside the definition of @TestPropertySource, you will see that locations is of type 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:

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.

like image 200
Gustavo Passini Avatar answered Sep 18 '22 06:09

Gustavo Passini