我的 Spring Boot 2 测试文件夹中有以下层次结构
test/
java/
package.name/
SpringBootTest.java
resources/
test.properties
SpringBootTest 有这样的代码
@RunWith (SpringRunner.class)
@TestPropertySource(locations = {"/resources/test.properties"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"management.port=0"})
public class SwaggerExtractorTest { ... }
我还尝试将locations = {“test.properties”}与位于java类本身旁边的文件以及其他一些路径一起使用,但它始终是错误类路径资源[... ] 无法打开,因为它不存在。
理想情况下,我喜欢 @TestPropertySource 的路径,我可以将其放置在测试代码层次结构的不同级别上而不进行更改,即相对于某个顶层。
设置此路径的正确方法是什么?
最佳答案
假设您引用的 test/resources
文件夹是 Maven/Gradle 构建使用的 src/test/resources
文件夹...
@TestPropertySource("/test.properties")
和 @TestPropertySource("classpath:test.properties")
是等效的,并且两者都应该适用于引用 test.properties
文件位于类路径的根目录中。
关于java - 使用 @TestPropertySource 正确定义测试属性的路径 (Spring Boot 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56933129/