Correctly define path to test properties using @TestPropertySource (Spring Boot 2)

半腔热情 提交于 2019-12-11 17:35:18

问题


I have a following hierarchy in my Spring Boot 2 test folder

 test/
      java/
           package.name/
                        SpringBootTest.java

       resources/
                 test.properties

SpringBootTest has a code like

@RunWith (SpringRunner.class)
@TestPropertySource(locations = {"/resources/test.properties"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
             properties = {"management.port=0"})
public class SwaggerExtractorTest { ... } 

I also tried to use locations = {"test.properties"} with the file located just next to the java class itself, and some other paths, but it was always the error class path resource [...] cannot be opened because it does not exist.

Ideally I like to have path for @TestPropertySource that I can place on different levels of test code hierarchy without change, that is, relative to some top level.

What would be a correct way to set this path?


回答1:


Assuming the test/resources folder you are referring to is the src/test/resources folder used by your Maven/Gradle build...

@TestPropertySource("/test.properties") and @TestPropertySource("classpath:test.properties") are equivalent, and both should work for referencing a test.properties file in the root of the classpath.



来源:https://stackoverflow.com/questions/56933129/correctly-define-path-to-test-properties-using-testpropertysource-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!