145

I have a file data.xml in src/test/resources/.

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

Rodrigue
  • 3,465
  • 2
  • 36
  • 49
Aaron Yodaiken
  • 17,660
  • 30
  • 95
  • 176

5 Answers5

212

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

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 source is 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.

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

Mitchell
  • 32,141
  • 6
  • 39
  • 35
  • 9
    I had to add a `getClassLoader` to the instruction. The result was `Source.fromURL(getClass.getClassLoader.getResource("simulation.json"))` – Moebius Jan 12 '17 at 17:30
  • 1
    Confirmed Moebius comment `getClassLoader` is needed. Without it the path includes the class hierarchy directory of the test class. Something like `~/lighthouse/target/scala-2.12/test-classes/com/mycompany/myapp/module1/utils/blabla/`. Using `getClass.getClassLoader.getResource()` the portion `com/mycompany/myapp/module1/utils/blabla/` is removed – Polymerase Apr 04 '18 at 20:48
33

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

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

Community
  • 1
  • 1
Neil
  • 3,994
  • 3
  • 34
  • 36
25

sbt copies files from src/test/resources to target/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.txt was directly under the folder src/test/resources. Add any subdirectories, otherwise.

Jacek Laskowski
  • 64,943
  • 20
  • 207
  • 364
Nick A Miller
  • 1,285
  • 1
  • 13
  • 19
0

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

testCompile "com.google.guava:guava:18.0"
oseiskar
  • 2,882
  • 2
  • 17
  • 22
-4

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")
heralight
  • 622
  • 1
  • 5
  • 11
  • The files under `src/test/resources` are in test's CLASSPATH so tests can access it without the code being aware of the build directory structure. – Jacek Laskowski Jul 27 '14 at 11:02
  • 2
    This might be useful though, if one needs to enumerate resources (i.e. the code does not have their names fixed, but will use any files placed under there). – akauppi Jan 23 '15 at 12:53
  • @akauppi this can be done with http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/core/io/support/PathMatchingResourcePatternResolver.html – dk14 Aug 13 '16 at 18:50
  • this will get you in trouble when you deploy your app as a fat jar – Alex Monras Jul 09 '19 at 14:02