Maven-覆盖测试资源文件夹 [英] Maven - Override test resource folder
问题描述
我在Maven和Eclipse中的测试遇到问题.
I have a problem with my tests in Maven and Eclipse.
我从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");
当我这样做时,我有一个Null指针异常并进行调试,我看到该路径为
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
此外,我认为重要的一点是我重写了pom中的测试目录,因为我们需要在特殊位置放置测试源.我们以这种方式做到了:
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>
所以我的问题是:为什么用Maven运行测试没有得到正确的工作目录?我可以定义一个特定的文件夹来读取一些文件吗?我尝试过
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()
但是当我打印绝对路径时,我又有了C:\Users\myUser\AppData\Local...\myFile
but when I printed the absolute path I had C:\Users\myUser\AppData\Local...\myFile
again
我没有机会遵守约定,因为我说的是一个从未使用过Maven的大型系统,因此无法更改所有目录结构.不幸的是,这不是我的愿望.
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.
此外,当我打印"user.dir"时,我有:C:\Users\myUser\AppData\Local\Temp
In addition, when I print my "user.dir" I have: C:\Users\myUser\AppData\Local\Temp
Edit2:我的项目路径在另一个分区E:\work\myProject
my project path is in another partition E:\work\myProject
推荐答案
尝试将其添加到pom.xml
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>
将其添加到build部分,它使您可以指定testResource文件夹的位置以及测试类的位置.考虑考虑检查此链接,还有一个示例此处,它可以帮助您创建pom.xml
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.
请记住有资源部分和testResource部分,您应该修改第二个指向正确的站点.
Remember there is resource section and testResource section, you should modify the second one pointing to the correct site.
还可以考虑使用final File javaFolder = new File("classpath:/WEB-INF/test/file");
这篇关于Maven-覆盖测试资源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!