Reputation: 361
I have a problem with my tests in Maven and Eclipse.
I run my test suite from Eclipse and all works well, but when I run
mvn test
I have an exception and it appears when I'm trying to read some files in test cases like this:
final File javaFolder = new File("WEB-INF/test/file");
When I do that I have a Null Pointer Exception and debugging I saw that the path is
C:\Users\myUser\AppData\Local\Temp\WEB-INF\test\file
In addition, I think that is important to say that I overwrote the test directory in my pom, because we need test sources in a special place. We did it in this way:
<testSourceDirectory>WEB-INF/test/java</testSourceDirectory>
So my question is: Why running tests with Maven is not getting the correct working directory? Could I define a specific folder to read some files? I tried with things like
getClass().getResource("myFile").getFile()
but when I printed the absolute path I had C:\Users\myUser\AppData\Local...\myFile
again
Edit: I don't have a chance to follow the convention because I'm talking about a big system that has never used Maven so can't change all the directory structure. It is not my desition, sadly.
In addition, when I print my "user.dir" I have: C:\Users\myUser\AppData\Local\Temp
Edit2: my project path is in another partition E:\work\myProject
Upvotes: 12
Views: 13848
Reputation: 106
I think your resources directory is different too. So make sure you have setup your resource directory (not only your source directory) and load your file using getResourcesAsStream
Upvotes: 0
Reputation: 19543
Have your tried adding this to your pom.xml
<testResources>
<testResource>
<directory>${basedir}/src/main/WEB-INF/test/file</directory>
</testResource>
<testResource>
<directory>${basedir}/src/main/WEB-INF/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
Add this to the build section, it let you specify the location for the testResource folder and where the test classes are. Consider checking this link as well, there is an example here that could help you to create the pom.xml.
Remember there is resource section and testResource section, you should modify the second one pointing to the correct site.
Also consider using final File javaFolder = new File("classpath:/WEB-INF/test/file");
Upvotes: 15
Reputation: 1322
The basic tenet of maven is convention over configuration. You may want to ask a honest question why you had to break out of the well established convention.
Did you try "src/main/webapp/WEB-INF/test/file"
Update: I think you should log the System.getProperty("user.dir") in your test case and make sure that the file is relative to the printed directory.
Upvotes: 0