How to access test resources in Scala?

后端 未结 5 886
难免孤独
难免孤独 2020-12-04 07:58

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

5条回答
  •  执笔经年
    2020-12-04 08:52

    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.

提交回复
热议问题