Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Override test resource folder

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

like image 936
MarcosTonina Avatar asked Feb 12 '16 17:02

MarcosTonina


1 Answers

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");

like image 57
Koitoer Avatar answered Oct 31 '22 21:10

Koitoer