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?
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.
This fixed it for me.
Make sure your test resources folder is set up in sources. Below are the instructions for IntelliJ:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.