Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将文件传递到testng测试方法?_Java_Unit Testing_Annotations_Testng - Fatal编程技术网

Java 如何将文件传递到testng测试方法?

Java 如何将文件传递到testng测试方法?,java,unit-testing,annotations,testng,Java,Unit Testing,Annotations,Testng,我有以下目录结构: 更新结构 src | -test | -java | | | -SomeXmlTest.java | -resources | -someXmlContent.xml 我认为这是一种糟糕的做法: new File(C:\Project ... \src\resources\someXmlContent.xml) // or magic dance like th

我有以下目录结构:

更新结构

src
 |
 -test
     |
     -java
     |   |
     |   -SomeXmlTest.java
     |
     -resources
          |
          -someXmlContent.xml
我认为这是一种糟糕的做法:

new File(C:\Project ... \src\resources\someXmlContent.xml)

// or magic dance like that
File currentDir = new File(System.getProperty("user.dir");
File parent = currentDir.getParent();
for (File file:parent.listFiles()){
    //find resources directory and iterate over it files
}
我相信有一个简单的注释基础解决方案,例如,我只是在一些注释中编写“src\resources\someXmlContent.xml”,并测试自动解析路径,但我如何做到这一点呢?

使用资源而不是文件。通过这种方式,您可以将应用程序打包到一个jar中,并检索jar中的资源

使用InputStream而不是文件/FileInputStream:

InputStream in = getClass().getResourceAsStream("/someXml.xml");

这是一个比文件更通用的解决方案。

如果你能重构你的代码!它应该采用输入流而不是文件。然后使用内存流(例如apache commons IOUtils)。通过这种方式,您不必访问任何外部资源,但是testng测试如何从jar执行呢?我想在Maven编译时执行测试。问题是从testng而不是从应用程序访问文件;它们可能在文件系统中,但也有类似“../x.jar!y/z/u.properties”的URL。