File object creation fails when running code as Junit test in Eclipse/STS
In my code, I'm creating a File object based on relative path to file in source code folder. Everything works perfectly when running this code under Tomcat.
When I run exactly the same code as JUnit/integration test using STS/Eclipse Run... and selecting JUnit, the test fails because it can't find the file based on same relative path that was working when code was running under Tomcat.
This is how the File is tried to be created in the code:
new File(getClass().getResource(relatativePathToTheFile).toURI())
I was trying to solve this by first figuring out the current path when the junit test is being run by using this code:
System.out.println(this.getClass().getClassLoader().getResource(".").toURI().toString());
It tells me that the current folder should be c:CODEmyProjectNamebinmain, when running the code as part of the integration test.
the file I'm trying to access from the code is here: c:CODEmyProjectNamesrcmainwebappWEB-INFresourcesnameOfTheFile.test
Based on this, I updated my relative path being used in code being run to following:
/../../src/main/webapp/WEB-INF/resources/
By "/../../" I'm trying to traverse two steps towards the root folder and from there start to navigate towards resources-folder. For some reason, even this update did not solve the issues - I'm pretty sure that the path is right.
Maybe, for some reason, the path is just not accessible when running unit tests. Maybe, there's some Gradle configuration or STS/Junit configuration to be done to sort that out.
All my integration tests are in folder ../src/integrationTest. All my code is in folder structure starting from../src/main/
java eclipse unit-testing integration-testing filepath
add a comment |
In my code, I'm creating a File object based on relative path to file in source code folder. Everything works perfectly when running this code under Tomcat.
When I run exactly the same code as JUnit/integration test using STS/Eclipse Run... and selecting JUnit, the test fails because it can't find the file based on same relative path that was working when code was running under Tomcat.
This is how the File is tried to be created in the code:
new File(getClass().getResource(relatativePathToTheFile).toURI())
I was trying to solve this by first figuring out the current path when the junit test is being run by using this code:
System.out.println(this.getClass().getClassLoader().getResource(".").toURI().toString());
It tells me that the current folder should be c:CODEmyProjectNamebinmain, when running the code as part of the integration test.
the file I'm trying to access from the code is here: c:CODEmyProjectNamesrcmainwebappWEB-INFresourcesnameOfTheFile.test
Based on this, I updated my relative path being used in code being run to following:
/../../src/main/webapp/WEB-INF/resources/
By "/../../" I'm trying to traverse two steps towards the root folder and from there start to navigate towards resources-folder. For some reason, even this update did not solve the issues - I'm pretty sure that the path is right.
Maybe, for some reason, the path is just not accessible when running unit tests. Maybe, there's some Gradle configuration or STS/Junit configuration to be done to sort that out.
All my integration tests are in folder ../src/integrationTest. All my code is in folder structure starting from../src/main/
java eclipse unit-testing integration-testing filepath
1
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway
– MadProgrammer
Dec 19 '18 at 23:02
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
1
And as I saidFile(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose ofgetClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do
– MadProgrammer
Dec 19 '18 at 23:08
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16
add a comment |
In my code, I'm creating a File object based on relative path to file in source code folder. Everything works perfectly when running this code under Tomcat.
When I run exactly the same code as JUnit/integration test using STS/Eclipse Run... and selecting JUnit, the test fails because it can't find the file based on same relative path that was working when code was running under Tomcat.
This is how the File is tried to be created in the code:
new File(getClass().getResource(relatativePathToTheFile).toURI())
I was trying to solve this by first figuring out the current path when the junit test is being run by using this code:
System.out.println(this.getClass().getClassLoader().getResource(".").toURI().toString());
It tells me that the current folder should be c:CODEmyProjectNamebinmain, when running the code as part of the integration test.
the file I'm trying to access from the code is here: c:CODEmyProjectNamesrcmainwebappWEB-INFresourcesnameOfTheFile.test
Based on this, I updated my relative path being used in code being run to following:
/../../src/main/webapp/WEB-INF/resources/
By "/../../" I'm trying to traverse two steps towards the root folder and from there start to navigate towards resources-folder. For some reason, even this update did not solve the issues - I'm pretty sure that the path is right.
Maybe, for some reason, the path is just not accessible when running unit tests. Maybe, there's some Gradle configuration or STS/Junit configuration to be done to sort that out.
All my integration tests are in folder ../src/integrationTest. All my code is in folder structure starting from../src/main/
java eclipse unit-testing integration-testing filepath
In my code, I'm creating a File object based on relative path to file in source code folder. Everything works perfectly when running this code under Tomcat.
When I run exactly the same code as JUnit/integration test using STS/Eclipse Run... and selecting JUnit, the test fails because it can't find the file based on same relative path that was working when code was running under Tomcat.
This is how the File is tried to be created in the code:
new File(getClass().getResource(relatativePathToTheFile).toURI())
I was trying to solve this by first figuring out the current path when the junit test is being run by using this code:
System.out.println(this.getClass().getClassLoader().getResource(".").toURI().toString());
It tells me that the current folder should be c:CODEmyProjectNamebinmain, when running the code as part of the integration test.
the file I'm trying to access from the code is here: c:CODEmyProjectNamesrcmainwebappWEB-INFresourcesnameOfTheFile.test
Based on this, I updated my relative path being used in code being run to following:
/../../src/main/webapp/WEB-INF/resources/
By "/../../" I'm trying to traverse two steps towards the root folder and from there start to navigate towards resources-folder. For some reason, even this update did not solve the issues - I'm pretty sure that the path is right.
Maybe, for some reason, the path is just not accessible when running unit tests. Maybe, there's some Gradle configuration or STS/Junit configuration to be done to sort that out.
All my integration tests are in folder ../src/integrationTest. All my code is in folder structure starting from../src/main/
java eclipse unit-testing integration-testing filepath
java eclipse unit-testing integration-testing filepath
asked Dec 19 '18 at 22:59
VkaVka
76315
76315
1
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway
– MadProgrammer
Dec 19 '18 at 23:02
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
1
And as I saidFile(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose ofgetClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do
– MadProgrammer
Dec 19 '18 at 23:08
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16
add a comment |
1
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway
– MadProgrammer
Dec 19 '18 at 23:02
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
1
And as I saidFile(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose ofgetClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do
– MadProgrammer
Dec 19 '18 at 23:08
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16
1
1
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway– MadProgrammer
Dec 19 '18 at 23:02
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway– MadProgrammer
Dec 19 '18 at 23:02
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
1
1
And as I said
File(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose of getClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do– MadProgrammer
Dec 19 '18 at 23:08
And as I said
File(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose of getClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do– MadProgrammer
Dec 19 '18 at 23:08
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16
add a comment |
1 Answer
1
active
oldest
votes
I eventually fixed this by myself by relocating the files under src/main/resources. Previously they were totally out of this folder. After this, the same, updated, relative file path works while running the code under Tomcat or via STS Junit test runner.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53860331%2ffile-object-creation-fails-when-running-code-as-junit-test-in-eclipse-sts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I eventually fixed this by myself by relocating the files under src/main/resources. Previously they were totally out of this folder. After this, the same, updated, relative file path works while running the code under Tomcat or via STS Junit test runner.
add a comment |
I eventually fixed this by myself by relocating the files under src/main/resources. Previously they were totally out of this folder. After this, the same, updated, relative file path works while running the code under Tomcat or via STS Junit test runner.
add a comment |
I eventually fixed this by myself by relocating the files under src/main/resources. Previously they were totally out of this folder. After this, the same, updated, relative file path works while running the code under Tomcat or via STS Junit test runner.
I eventually fixed this by myself by relocating the files under src/main/resources. Previously they were totally out of this folder. After this, the same, updated, relative file path works while running the code under Tomcat or via STS Junit test runner.
answered Dec 28 '18 at 16:18
VkaVka
76315
76315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53860331%2ffile-object-creation-fails-when-running-code-as-junit-test-in-eclipse-sts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
File(getClass().getResource(...))
worries me. Imbedded resources are not (always) "files", they could be embedded within a Jar file, which makes then inaccessible via the normal file API. As a "general" recommendation, you shouldn't be trying to write to "embedded resources" anyway– MadProgrammer
Dec 19 '18 at 23:02
The file I'm trying to access is certainly a file. I'm 100% sure about that. Also, like I said in the very first sentence of my question, I'm able to read the file perfectly when running the code in Tomcat. So, basically there should not be any problems reading this file. The problem is that somehow I'm not able to find the file anymore when running the same code under STS/Eclipse Unit-test runner.
– Vka
Dec 19 '18 at 23:07
1
And as I said
File(getClass().getResource(...))
is worrying. It suggests that you don't understand the purpose ofgetClass().getResource(...)
. While I recognise the fact that tomcat unpacks the WAR, not all servlet containers do– MadProgrammer
Dec 19 '18 at 23:08
While playing around with this I was able to get everything working just by changing the old relative file paths to absolute paths. In the end this wasn't the perfect solution, so I ended up relocating my files according to the answer below.
– Vka
Dec 28 '18 at 16:16