测试时(Springboot)如何激活application.properties中使用的环境变量?

问题描述 投票:0回答:1

我想在我的 Springboot (3.2.4) 应用程序中进行测试,但我有一个问题。当我启动测试时,它失败并出现异常,因为 application.properties 中使用的环境变量未在测试中加载。

例如我的 application.properties 中有这个:

spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PSW}
测试失败,因为 ${DB_USER} 和 ${DB_PSW} 没有被环境变量替换。

是否有一些简单的修复可以在使用测试时加载所有环境变量? 谢谢你的帮助。

java spring-boot testing environment-variables
1个回答
0
投票

如果您将属性添加到 src/main/resources/application.properties,则还必须覆盖 src/test/resources/application.properties 中的属性,因此只需将具有属性的相同文件添加到测试包即可 https://www.baeldung.com/spring-tests-override-properties

© www.soinside.com 2019 - 2024. All rights reserved.