How to access test resources in Scala?
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.
Related videos on Youtube
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, 2020Comments
-
Aaron Yodaiken over 3 years
I have a file
data.xml
insrc/test/resources/
.How can I read that file into a new
FileReader
in my testdata.scala
insrc/test/scala/
? -
Jacek Laskowski over 9 yearsThe 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 about 9 yearsThis 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 over 7 years@akauppi this can be done with docs.spring.io/spring-framework/docs/2.5.x/api/org/…
-
Moebius over 7 yearsI had to add a
getClassLoader
to the instruction. The result wasSource.fromURL(getClass.getClassLoader.getResource("simulation.json"))
-
Polymerase about 6 yearsConfirmed 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/
. UsinggetClass.getClassLoader.getResource()
the portioncom/mycompany/myapp/module1/utils/blabla/
is removed -
Alex Monras almost 5 yearsthis will get you in trouble when you deploy your app as a fat jar
-
yuranos about 2 yearsWasn'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).