0

I am writing a test for a spring boot application and I am facing a weird behaviour with the placeholder replacement in the profile-specific application properties file.

So, in my settings.xml I have my DB creds set up like

...
<profiles>
    <profile>
        <properties>
            <db.user>MyLogin</db.user>
            <db.password>MyPassword</db.password>
            <db.url>my_db_connection_url</db.url>
        </properties>
    </profile>
</profiles>
...

So there is only one profile in settings.xml. In the application I have a profile-specific application properties file like that:

application-myprofile.properties :

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

and while I am starting the spring boot application with a specific profile myprofile - it all works just fine, creds are being pulled from the settings.xml.

However, for the tests I have a slightly different setup.

I have a test class like that:

@SpringBootTest
@ActiveProfiles("mytestprofile")
public class MyTest {
    ....
}

and a corresponding test profile properties file inside test/resources like that

application-mytestprofile.properties

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

and in this case the placeholders are not being replaced by the values from settings.xml. However, if I replace the content of application-mytestprofile.properties with

spring.datasource.url=my_db_connection_url
spring.datasource.username=MyLogin
spring.datasource.password=MyPassword

it works just fine. Also, if I put the placeholders inside the application.properties:

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

it also works.

The question is: why is that? How can I make a profile-specific application properties file work with settings.xml in tests?

Vadim Lopatkin
  • 134
  • 1
  • 7

1 Answers1

0

I don't have all the details about your project setup, but it looks like you have a test folder structure like this:

test 
    -> java 
            -> resources

while I think it should be

test 
    -> java
    -> resources

So, move your test resources folder one level up, and it should work.