Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use relative path following placeholder in @TestPropertySource locations #23544

Closed
Pancor opened this issue Aug 29, 2019 · 3 comments
Closed
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@Pancor
Copy link

Pancor commented Aug 29, 2019

Affects: Spring Boot version: 2.1.1.RELEASE with Spring-test version: 5.1.3.RELEASE

I'm trying to add custom path to application.properties file for one of my class test with use of @TestPropertySource like this:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "file:${user.dir}/../config/application.properties")
public class AuthControllerITest {
    //some code
}

And I have an error caused by FileNotFoundException which says that it can not find this path: config/application.properties. When I remove this ".." characters (so i've got file:${user.dir}/config/application.properties), then it gives me this path: /home/pawel/projekty/simulatorserver/server/config/application.properties, but I want to get to this path: /home/pawel/projekty/simulatorserver/config/application.properties (without server directory).

I did some investigation and I found out that org.springframework.util.StringUtils#cleanPath(String path) removes ${user.dir} from path.

I guess it should first resolve ${user.dir} to path, and then remove one directory from it.

For now I'm satisfied with this solution:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "file:../config/application.properties")
public class AuthControllerITest {
    //some code
}

which gives me proper output: /home/pawel/projekty/simulatorserver/config/application.properties.

Stacktrace:

java.lang.IllegalStateException: Failed to load ApplicationContext

	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
	at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Failed to add PropertySource to Environment
	at org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:201)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:104)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
	... 24 more
Caused by: java.io.FileNotFoundException: config/application.properties (No such file or directory)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
	at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
	at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
	at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:173)
	at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
	at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
	at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
	at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59)
	at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:86)
	at org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:197)
	... 27 more
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 29, 2019
@sbrannen sbrannen added the in: test Issues in the test module label Aug 29, 2019
@sbrannen
Copy link
Member

Thanks for raising the issue.

It's certainly a unique problem that only appears to manifest itself if you have a placeholder immediately followed by /../ in the supplied path.

@TestPropertySource(locations = "file:../config/application.properties")

That is indeed the best way to reference the parent directory for the currently executing project. So I'd recommend sticking with that.

As for the general case where a placeholder is followed by /../ in the supplied path, I'm not sure that we can "fix" that without introducing a breaking change, but we'll investigate our options.

@sbrannen sbrannen added status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 29, 2019
@sbrannen sbrannen self-assigned this Aug 29, 2019
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Aug 29, 2019
@sbrannen sbrannen changed the title StringUtils#cleanPath(String path) removes property value when TOP_PATH is used Cannot use relative path following placeholder in @TestPropertySource locations Aug 29, 2019
sbrannen added a commit that referenced this issue Aug 30, 2019
This commit introduces a collection of @nested integration tests that
verify proper support for @TestPropertySource with explicit properties
files declared with absolute paths, relative paths, and placeholders in
the classpath and in the file system.

See gh-23544
sbrannen added a commit that referenced this issue Aug 30, 2019
This commit ensures that locations to resources configured via
@ContextConfiguration & @TestPropertySource are consistently cleaned
using StringUtils.clean().

See gh-23544
@sbrannen sbrannen removed the in: core Issues in core modules (aop, beans, core, context, expression) label Aug 30, 2019
@sbrannen
Copy link
Member

This has been fixed in master in ab779eb.

Feel free to try it out in the next 5.2 snapshot build.

@sbrannen sbrannen added type: bug A general bug and removed status: pending-design-work Needs design work before any code can be developed labels Aug 30, 2019
@sbrannen sbrannen added this to the 5.2 RC2 milestone Aug 30, 2019
@Pancor
Copy link
Author

Pancor commented Aug 31, 2019

Thanks, great to hear that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants