【发布时间】:2019-11-17 20:42:46
【问题描述】:
我的 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 { ... }
我还尝试将位置 = {"test.properties"} 与位于 java 类本身旁边的文件和其他一些路径一起使用,但它始终是错误 类路径资源 [... ] 无法打开,因为它不存在。
理想情况下,我希望有 @TestPropertySource 的路径,我可以将其放置在不同级别的测试代码层次结构上而无需更改,即相对于某个顶层。
设置此路径的正确方法是什么?
【问题讨论】:
-
从该位置删除
/resources/。 -
确保
test.properties移动到target/test-classes。也许问题就在那里...... -
尝试 ::: @TestPropertySource("classpath:test.properties")
-
@M.Deinum 然后它会期望文件在测试文件旁边(相同的类路径)。但是把它移到那里也无济于事。
-
出于某种原因,我认为它的前缀是
classpath:(我讨厌星期一)。你应该使用classpath:test.properties。src/test/resources是测试类路径的根(就像src/test/java但对于.java文件)。
标签: java spring spring-boot spring-test