运行测试时,只能在调试模式下从 Spring 启动的应用程序.属性文件中读取值



我有一个spring-boot 2.7 kotlin应用程序,当我在调试模式下运行测试时,测试只能从属性文件中读取值,而在正常模式下,当我只是运行测试时我会收到一个错误,说用户名不应该为空。应用程序正在使用@Value注释来读取属性和

@PropertySource("classpath:application.properties")

还添加了。这就是我使用@Value的方式

@Service
class MyService(
@Value("${Something.username}") private val user: String,
@Value("${Something.password}") private val pwd: String,
@Value("${accesstoken.endpoint}") val tokenEndpoint: String
) {
......
}

欢呼,

es

如果有人有同样的问题,他们应该添加

@TestPropertySource("classpath:application.properties")

他们的测试。这为我解决了问题,我现在可以使用run test命令或mvn test命令运行测试。

为什么这样做和添加

@PropertySource("classpath:application.properties")

去主课对我来说不是个谜吗?

最新更新