How to access test resources in Scala?

82,690

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
82,690

Related videos on Youtube

Aaron Yodaiken
Author by

Aaron Yodaiken

John Jay Scholar of Economics at Columbia. Love coding, designing, and learning to make the world a better place. Learn more about what I’m up to at www.aaronyodaiken.com.

Updated on November 23, 2020

Comments

  • Aaron Yodaiken
    Aaron Yodaiken over 3 years

    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/?

  • Jacek Laskowski
    Jacek Laskowski over 9 years
    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.
  • akauppi
    akauppi about 9 years
    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).
  • dk14
    dk14 over 7 years
  • Moebius
    Moebius over 7 years
    I had to add a getClassLoader to the instruction. The result was Source.fromURL(getClass.getClassLoader.getResource("simulati‌​on.json"))
  • Polymerase
    Polymerase about 6 years
    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/my‌​app/module1/utils/bl‌​abla/. Using getClass.getClassLoader.getResource() the portion com/mycompany/myapp/module1/utils/blabla/ is removed
  • Alex Monras
    Alex Monras almost 5 years
    this will get you in trouble when you deploy your app as a fat jar
  • yuranos
    yuranos about 2 years
    Wasn't the case for me with class hierarchy, but with getClassLoader a slash in resource name is not needed, while without getClassLoader it is. If not used that way I"d always get a null(resource not found).

Related