我正在为Spring Boot应用程序编写测试,并且由于配置文件特定的应用程序属性文件中的占位符替换而面临奇怪的行为。
因此,在我的settings.xml中,我的数据库凭据设置为
...
<profiles>
<profile>
<properties>
<db.user>MyLogin</db.user>
<db.password>MyPassword</db.password>
<db.url>my_db_connection_url</db.url>
</properties>
</profile>
</profiles>
...
因此,settings.xml中只有一个配置文件。在应用程序中,我有一个特定于配置文件的应用程序属性文件,如下所示:
application-myprofile.properties
:
spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@
当我使用特定的配置文件myprofile
启动spring boot应用程序时-一切正常,但从settings.xml中提取了凭据。
但是,对于测试,我的设置稍有不同。
我有一个这样的测试班:
@SpringBootTest
@ActiveProfiles("mytestprofile")
public class MyTest {
....
}
以及test/resources
中的相应测试配置文件属性文件
application-mytestprofile.properties
spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@
,在这种情况下,占位符不会被settings.xml
中的值替换。但是,如果我用
application-mytestprofile.properties
的内容
spring.datasource.url=my_db_connection_url
spring.datasource.username=MyLogin
spring.datasource.password=MyPassword
它工作正常。另外,如果我将占位符放在application.properties
内:
spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@
它也可以工作。
问题是:为什么呢?在测试中,如何使特定于配置文件的应用程序属性文件与settings.xml
一起使用?
答案 0 :(得分:0)
我没有有关您的项目设置的所有详细信息,但看起来您具有这样的测试文件夹结构:
test
-> java
-> resources
我认为应该
test
-> java
-> resources
因此,将您的测试资源文件夹上移一个级别,它应该可以工作。