如何访问 Scala 中的测试资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5285898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:53:54  来源:igfitidea点击:

How to access test resources in Scala?

scalasbt

提问by Aaron Yodaiken

I have a file data.xmlin src/test/resources/.

我有一个文件data.xmlsrc/test/resources/.

How can I read that file into a new FileReaderin my test data.scalain src/test/scala/?

如何FileReader在我的测试data.scala中将该文件读入新文件src/test/scala/

回答by Mitchell

Resources are meant to be accessed using the special getResourcestyle methods that Java provides. Given your example of data.xmlbeing in $SBT_PROJECT_HOME/src/test/resources/, you can access it in a test like so:

资源旨在使用getResourceJava 提供的特殊样式方法进行访问。鉴于你的例子data.xml是在$SBT_PROJECT_HOME/src/test/resources/,你可以像这样的测试访问它:

import scala.io.Source

// The string argument given to getResource is a path relative to
// the resources directory.
val source = Source.fromURL(getClass.getResource("/data.xml"))

Of course that sourceis now just a normal Scala IO object so you can do anything you want with it, like reading the contents and using it for test data.

当然,source它现在只是一个普通的 Scala IO 对象,所以你可以用它做任何你想做的事情,比如读取内容并将其用于测试数据。

There are other methods to get the resource as well (for example as a stream). For more information look at the getResourcemethods on the Java Docs: Class.

还有其他方法可以获取资源(例如作为流)。有关更多信息,请查看Java Docs: ClassgetResource上的方法。

回答by Neil

Another alternative (especially if you need to access resource as a File); is to obtain it's path via:

另一种选择(特别是如果您需要将资源作为 访问File);是通过以下方式获得它的路径:

val path = getClass.getResource("/testData.txt").getPath
val file = new File(path)

as has been pointed out in Scala get file path of file in resources folder

正如Scala中指出的那样获取资源文件夹中文件的文件路径

回答by Nick A Miller

sbt copies files from src/test/resourcesto target/scala-[scalaVersion]/test-classes.

从SBT文件复制src/test/resourcestarget/scala-[scalaVersion]/test-classes

You can access the resources in your tests as follows:

您可以按如下方式访问测试中的资源:

Source.fromURL(getClass.getResource("/testData.txt"))

It does assume that testData.txtwas directly under the folder src/test/resources. Add any subdirectories, otherwise.

它确实假设它testData.txt直接位于文件夹下src/test/resources。添加任何子目录,否则。

回答by oseiskar

And in cases where getClass.getResourcedoes not work (don't know nor care when or why exactly), com.google.common.io.Resources.getResourcefrom Google Guava usually does

getClass.getResource不起作用的情况下(不知道也不关心何时或为什么),com.google.common.io.Resources.getResource来自谷歌番石榴通常会

testCompile "com.google.guava:guava:18.0"

回答by heralight

To know where you are in file system during test, you can do something like this in a dummy test:

要在测试期间了解您在文件系统中的位置,您可以在虚拟测试中执行以下操作:

 import scala.collection.JavaConversions._
  for(file <- new File(".").listFiles ){
   println(file.getAbsolutePath)
  }

Then, when you know your path, in your test you can use it as:

然后,当你知道你的路径时,在你的测试中你可以将它用作:

new File("./src/test/resources/yourfile.xml")