2

I have spring application that uses three datasources, the details are stored in my application.properties file. I am looking to use environment variables for the usernames and passwords, see below:

spring.datasource1.username=${DB1_Username}
spring.datasource1.password=${DB1_Password}

This is works fine when i start the application normally but not when i run the tests. I get the following error when i try to run a test:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '${DB1_Username}'.

Can environment variables not be used in the application.properties file when running tests? Or am i doing something stupid?

spence
  • 166
  • 3
  • 11
  • how do you run your test ? – JEY Jun 26 '17 at 17:05
  • i am using the gradle build command. When i use the gradle build --debug i see the error above. – spence Jun 27 '17 at 10:46
  • They can be used but might not be set in the gradle process while they are set in your start-up. You can also overwrite specific variables in test via annotations as to not rely on the environment to be setup for test – pandaadb Jun 27 '17 at 11:09
  • This particular use case is clearly documented in Boot docs – Abhijit Sarkar Apr 18 '19 at 05:55

2 Answers2

-1

may be your application.properties file not located at right location. and you need to configure the property file into spring boot configuration. for application.properties file configuration you can refer bellow link :-

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
-1

Spring automatically takes up the property file from

src/test/resources/application.properties

when you are running test.If you are using the @TestPropertySource annotation like @TestPropertySource("classpath:application.properties") if you keep the test application.properties in the test resources folder , it should get automatically picked up.

If you are having issues while resolving :

spring.datasource1.username=${DB1_Username}
spring.datasource1.password=${DB1_Password}

try providing the values in application.properties and put it in the directory src/test/resources/ .

Another method is to use spring profiles, you can specify spring profiles to run tests.Specify a 'test' spring profile to run all your tests and provide an application-test.properties or application-test.yml file which contains the necessary test properties. Read for more details: Load different application.yml in SpringBoot Test

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39