在Spring Boot测试类上加载属性

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

我的Spring Boot项目结构如下:

我在src/test下的Main类中有我的测试代码,在scr/test/resources下有一个测试专用属性文件 我想加载该文件,以便我可以从我的Main(测试)java类中读取它的属性,所以我做(在Main类中): enter image description here

但文件没有加载,至少我假设,因为当我尝试以这种方式读取属性时:@Value("${some.property.present.in.that.file}") private String prop; 那么prop总是空的。 我不明白为什么这个机制在我的src/main/javacode中工作但不在测试中,ApplicationContext应该在那里,主测试类用@SpringBootTest注释,你可以看到自己的目标,而official documentation说: enter image description here

对此有何建议?

java spring-boot
1个回答
0
投票

我相信如果你将测试移动到src / test / java / *,SpringBootTest将会看到它们而没有任何其他配置。

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