10

I am trying to use scalatest, but Intellij cannot recognize:

import org.scalatest._

Here is my build.sbt file, located in the same directory as my scalatest.jar file.

scalaVersion := "2.11.2"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"

Thanks

nietsnegttiw
  • 371
  • 1
  • 5
  • 16
  • 1
    Have you checked the sbt log of IntelliJ. Often this happens when a dependency is not correctly resolved (e.g. mispelled, wrong version etc.). You can also go to project settings and look into the libraries tab to see if ScalaTest is actually included. You won't need a `scalatest.jar` file, the whole point about managed dependencies is that sbt takes care of that. If you haven't checked `auto-import` when creating the project, you also must explicitly refresh the sbt build if you edit the `build.sbt`. – 0__ Jul 14 '15 at 20:36
  • I went into my project structure and altered this. While it seems to have resolved itself in Intellij, upon compilation "org.scalatest" is still unrecognized. – nietsnegttiw Jul 14 '15 at 20:40
  • 1
    Can you provide your complete `build.sbt`? You shouldn't need to alter the project structure by hand. Let IntelliJ do it for you based on `build.sbt` with auto-import enabled for your project. – 0__ Jul 14 '15 at 20:44
  • I have just started using sbt today, so I may not understand your question. Is there more to the build.sbt file than that? – nietsnegttiw Jul 14 '15 at 20:45
  • 2
    Ok, so above is your full sbt file. With that you should be fine. I suggest perhaps close IntelliJ, delete the `.idea` directory, and start over from fresh. Import the project (using the suggested sbt model) and make sure the auto-import is checked. Look for yellow warnings coming from sbt, should it have trouble resolving the dependency (perhaps a network error). Ok, here is another guess: The `import org.scalatest._` is in a source file __inside src/test/scala__, right? Because with `% "test"` you make ScalaTest only available to test sources! – 0__ Jul 14 '15 at 20:50
  • Wow, this works, thanks! – nietsnegttiw Jul 14 '15 at 20:58

1 Answers1

17

So you have by convention two source folders:

src/main/scala/...
src/test/scala/...

The first is shown blue, the second green in IntelliJ IDEA. The library dependencies in sbt are associated with either of these, so

"org.foo" % "bar_2.11" % "1.2.3"

Is a main dependency, available to main sources (and also test, because test depends on main). And

"org.foo" % "bar_2.11" % "1.2.3" % "test"

Is a test dependency, only available to test sources. The idea is that these are libraries which are not required for your product, but only to run the unit tests.


In your example, Scala-Test is only available to test sources, so trying to import it from main sources will fail.

0__
  • 66,707
  • 21
  • 171
  • 266
  • 1
    In short, make sure your test file is under `src/test/scala` or the import won't work correctly. – loxaxs Dec 31 '17 at 17:27