L Becker
L Becker

Reputation: 755

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

I'm writing an integration test method in scala (play framework). The test class is SourceIntegrationTest. I've placed a file, source.json, in /test/resources. I'm aware that "sbt copies files from src/test/resources to target/scala-[scalaVersion]/test-classes" as described in this answer. However, using the answer referenced there only works for me when running my test in IntelliJ. When I run sbt it:testOnly SourceIntegrationTest in terminal, my test fails with a NullPointerException. sbt cannot find source.json. How can I get sbt to find my file when running my integration test in terminal?

My test method looks like:

@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")
}

Upvotes: 1

Views: 1492

Answers (1)

Mahesh Chand
Mahesh Chand

Reputation: 3250

When you write an integration test, the test file is not placed in the test folder. You need to create another directory for integration test. Now the source folder will contain three directories main, test, it. And all integration test will be kept in it folder. You can read about it here

Upvotes: 1

Related Questions