我应该如何在Maven管理的Java项目中发现测试资源文件? [英] How should I discover test-resource files in a Maven-managed Java project?

查看:122
本文介绍了我应该如何在Maven管理的Java项目中发现测试资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用正常Maven结构的项目,例如

  module 
\ src
\ main
- java
- 资源
\ test
- java
- 资源

等。在 test / resources 下,我想为我正在编写的解析器保留一组测试输入文件,然后通过测试套件运行目录中的所有文件。如现在写的,测试代码从命令行工作,但在运行Eclipse JUnit插件时失败:

 文件文件=新文件(src / test / resources); 
file.list();

(实际上我使用的是一个 FilenameFilter ,但我正在尝试简化。)



在使用调试器进行单元测试后,问题证明是我正在构建点到 / path / to / workspace / myproj / src / test / resources ,而实际的文件驻留在 / path / to / workspace / myproj / modulename / src / test / resources (它是一个Maven多模块项目)。显然,从命令行运行 mvn test 这不是问题。



我猜我的问题是双重:一,我这样做错了?我看到很多人使用类加载器来发现资源,如这个问题,但是我不希望使用特定类型的所有资源,只是一个目录在test / resources下。二,如果这不是一个可怕的想法,首先,我有一个配置错误(例如它应该工作)?它是Eclipse的错,Maven问题,还是什么?

解决方案

一个窍门是将文件放在具有已知名称,通过类加载器获取此文件的URI,然后从此URI构造一个文件,然后获取父目录,并列出该目录的内容。所以这里是代码应该是什么样子,但是在test / src / resources中放置一个名为MY_TEST_FILE(或任何)的文件。

  URL myTestURL = ClassLoader.getSystemResource(MY_TEST_FILE); 
文件myFile = new File(myTestURL.toURI());
文件myTestDir = myFile.getParentFile();

然后,您可以访问您要查找的目录。


那就是说,如果没有更多的maven-y方式,我会感到惊讶。


I have a project that uses the normal Maven structure, e.g.

module
\ src
  \ main
    - java
    - resources
  \ test
    - java
    - resources

etc. Under test/resources, I'd like to keep a set of test input files for a parser I'm writing, then run all files in the directory through the test suite. As written now, the test code works from the command line, but fails when run through the Eclipse JUnit plugin:

File file = new File("src/test/resources");
file.list();

(I'm actually using a FilenameFilter, but I'm trying to simplify.)

The problem, after poking through the unit test with a debugger, turns out to be that the File I'm constructing points to /path/to/workspace/myproj/src/test/resources, whereas the actual files reside in /path/to/workspace/myproj/modulename/src/test/resources (it's a Maven multi-module project). Apparently, this isn't a problem when running mvn test from the command line.

I guess my question is two-fold: one, am I doing this wrong? I see a lot of people using the class loader to discover resources, as in this question, but I don't want all the resources of a particular type, just one directory under test/resources. Two, if this isn't a terrible idea in the first place, do I have a configuration error (e.g. it "should" work)? Is it Eclipse's fault, a Maven problem, or what?

解决方案

One trick would be to place a file in resources with a known name, get the URI of this file through the classloader, then construct a File from this URI, then get the parent, and list() the contents of that directory. Kind of a hack, but it should work.

So here's what the code should look like, but place a file called MY_TEST_FILE (or whatever) in test/src/resources

URL myTestURL = ClassLoader.getSystemResource("MY_TEST_FILE");
File myFile = new File(myTestURL.toURI());
File myTestDir = myFile.getParentFile();

Then you have access to the directory you're looking for.

That said, I'd be surprised if there's not a more 'maven-y' way to do it..

这篇关于我应该如何在Maven管理的Java项目中发现测试资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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