从Maven的JUnit测试用例中获取文件的最干净方法 [英] Cleanest way to get a File in a JUnit test case from Maven

查看:100
本文介绍了从Maven的JUnit测试用例中获取文件的最干净方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Maven项目B(它是项目A的子项目)中的测试代码具有类似的内容

My test code in Maven project B (which is a child of project A) has something like

String filePath = "src/main/webapp";
//do something with the filePath

当我从子级(即B级)运行项目时,上述测试用例运行良好,但是当我从父级项目A(即在父级进行mvn安装)运行时,此测试用例失败,因为显然没有名为父级下的"src/main/webapp"(但是在子级下可用).

The above test case runs fine when I run the project from child (i.e, level B) but when I run from Parent project A (i.e, doing a mvn install at parent level) this fails because obviously there is no folder called "src/main/webapp" under parent level (It is however available in child level).

我知道我可以做一些编码来检查是否从父/子模块运行了一个测试用例,但是显然我想知道其他人在遇到这个问题时做了什么?

I know I could do some coding do check if a test case is running from parent/child module but apparently I want to know what others have done when they have had this problem ?

不,我不能使用类路径(出于各种无聊的原因).

And no, I cant use the classpath instead (for various boring reasons).

我也尝试了相对路径,但是随后测试用例就开始了解太多了.真的有解决方案吗?

I also tried relative path but then the test case sort of starts to know too much. Is there actually a solution at all for this ?

更新(2月12日)-我在webapp文件夹下有一个web.xml,并在使用该web.xml的测试用例中创建了一个码头服务器.理想情况下,src/main/webapp不放置在类路径中. WAR插件使用它来打包WAR.现在,我尝试了一种替代方法,将我的web.xml放在src/main/resource/console-webapp/WEB-INF/web.xml目录中,并在maven war插件中更改了webXml属性.这似乎解决了我的问题.但是,在输出的WAR中,我有一个web.xml和另一个冗余的web.xml(由于它在类路径中而被复制了).

UPDATE (12/Feb) - I have a web.xml under webapp folder and create a jetty server in a test case using that web.xml. Ideally src/main/webapp is not placed in the classpath. It is used by the WAR plugin to package the WAR. Now, I tried an alternative where I put my web.xml in the src/main/resource/console-webapp/WEB-INF/web.xml directory and altered the webXml attribute in the maven war plugin. This seems to solve my problem. However, in the output WAR I have a web.xml and another redundant web.xml (which was copied because it is in the classpath).

我尝试了"packageExcludes"和"webResources/excludes",并获得了war插件以省略第二个web.xml,但是仍然复制了"console-webapp"目录(尽管为空).有什么方法可以告诉Maven War插件完全忽略目录(即,它的ant模式是什么?)

I have tried "packageExcludes" and "webResources/excludes" and got the war plugin to omit the second web.xml but still the directory "console-webapp" gets copied (although is empty). Is there any way to tell maven war plugin to ignore a directory completely (i.e, what is the ant pattern for that?)

推荐答案

Maven 2约定规定,所有测试资源(包括文件,例如您尝试在测试中使用的文件)必须存储在src/test/resources中文件夹.如果遵守此约定,则可以直接从类路径中获取文件.这些资源将包含在最终包装中(JAR,WAR,EAR ...).

The Maven 2 conventions state that all test resources, including files such as the one you are trying to use in your test, must be stored in the src/test/resources folder. If you respect this convention, you will be able to get your file from the classpath directly. These resources will not be included in the final packaging (JAR, WAR, EAR...).

当然,您可以更改目录,然后在pom.xml中指定新的测试资源目录:

Of course, you can change the directory and then specify the new test resources directory in your pom.xml:

<build>
    <testResources>
        <testResource>
            <directory>...</directory>
        </testResource>
    </testResources>
    ...

在您的问题中,您指定不能使用类路径.为什么这样?

In your question, you specified that you can't use the classpath. Why so?

这篇关于从Maven的JUnit测试用例中获取文件的最干净方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆