27

EDIT : It works if the file is in src/test/scala/tests/ but not in src/main/scala/mypackage/. Why?

I have tried solutions from topics with people having nearly the same issue but none of them work.

For details, I have this in build.sbt :

libraryDependencies ++= Seq(
   ...
  "org.scalatest" % "scalatest_2.10" % "2.2.1" % "test",
   ...

In Intellij, I have a class with:

import org.scalatest.{BeforeAndAfterAll, Suite}

with {BeforeAndAfterAll, Suite} in red so i guess scalatest is found

sbt package does not work either:

object scalatest is not a member of package org [error] import org.scalatest. {BeforeAndAfterAll, Suite}

I have already tried this :

  • sbt clean update
  • restart + invalidate cache of intellij
  • remove .idea/ and reimport
  • libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test" instead ogf the actual
  • magical ritual on my keyboard

Nothing works

Any ideas?

5
  • Why do you want to use scalatest in src/main?
    – Ben
    Commented Feb 18, 2016 at 18:51
  • remove the % "test" from the scalatest library dependency. As long as that's there, you won't get the required entry in the classpath during the compile phase.
    – philwalk
    Commented Feb 18, 2016 at 19:29
  • Did you fix this? I am getting the same error. Any advice would be helpful.
    – Srinivas
    Commented Nov 13, 2017 at 18:22
  • No, sorry Srinivas
    – Benjamin
    Commented Nov 13, 2017 at 19:00
  • I had this very issue with Maven in IntelliJ. I invalidated caches and restarted but it didn't help. I reimported Maven and it fixed it. Commented Jan 10, 2020 at 1:08

1 Answer 1

29

You should take a look at sbt project structure:

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
    java/
       <main Java sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
    java/
       <test Java sources>

Your scala tests should go under src/test/scala/.

If you really want to use scalatest in main (and you know what you are trying to do), try this:

  "org.scalatest" % "scalatest_2.10" % "2.2.1",

In your original build.sbt, "test" is the configuration and it means that scalatest will only be on the test classpath and it isn’t needed by the main sources. This is generally good practice for libraries because your users don’t typically need your test dependencies to use your library (ref).

1
  • in IntelliJ switch for a second from package view to project view, then You see the misery ;) Thanks for your help! Commented Jun 9, 2020 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.