我试图阅读一个文件"my_data.txt"坐在我的 /src/test/resources/data
文件夹。
––– src
–– main
–– test
––– resources
––– data
––– my_data.txt
我有以下一段代码做到这一点:
val filename = getClass.getResource("/src/test/resources/data/my_data.txt").getPath
当我汇编,该汇编进行得很顺利,但是,当我运行测试在闪避器,我收到以下错误信息:
java.lang.NullPointerException
at (xxxx.scala:128)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:454)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.FunSpec.withFixture(FunSpec.scala:1630)
at org.scalatest.FunSpecLike.invokeWithFixture$1(FunSpecLike.scala:452)
at org.scalatest.FunSpecLike.$anonfun$runTest$1(FunSpecLike.scala:464)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.FunSpecLike.runTest(FunSpecLike.scala:464)
at org.scalatest.FunSpecLike.runTest$(FunSpecLike.scala:446)
When I try to print the path that I am reading from I got `null` as an output.
另一个试图
除了上述的,我尝试了下列代码:
val ss = scala.io.Source.fromResource("/src/test/resources/data/my_data.txt")
ClassLoader.getSystemResource("/src/test/resources/data/my_data.txt").getPath
此外,我加入 src/test/resources/data
到资源 POM
文件
最后
为 此 提到的,我查过如果我有 .txt
包括在编译器(I am使用 MAC
)