sbt it:test 找不到 IntelliJ 可以找到的测试资源文件

sbt it:test cannot find test resource file that IntelliJ can

我正在用 Scala(游戏框架)编写集成测试方法。测试 class 是 SourceIntegrationTest。我在 /test/resources 中放置了一个文件 source.json。我知道 "sbt copies files from src/test/resources to target/scala-[scalaVersion]/test-classes" as described in this answer. 但是,使用此处引用的答案仅在 运行 在 IntelliJ 中测试时对我有用。当我在终端中 运行 sbt it:testOnly SourceIntegrationTest 时,我的测试因 NullPointerException 而失败。 sbt 找不到 source.json。 运行在终端中进行集成测试时,如何让 sbt 找到我的文件?

我的测试方法如下:

@Test
def testGetSource(): Unit = {
  val jsonSource: String = Source.fromInputStream(getClass.getClassLoader.getResourceAsStream("source.json")).mkString
  val json: JsValue = Json.parse(jsonSource)
  val source = controller.getSource(json)
  assertEquals(source.sourceName = "Premier")
}

写集成测试的时候,测试文件没有放在test文件夹下。您需要为集成测试创建另一个目录。现在源文件夹将包含三个目录 main、test、it。所有集成测试都将保存在它的文件夹中。你可以阅读它 here