I know there are already answered questions about this, but they didn't work for me, maybe because they are too old now.
So I'm creating a scala project on Intellij IDEA Ultimate (SDK 16.0.1). I compile it using sbt. I created a MainTest
class in src/test/scala
with the following content :
import org.scalatest.funsuite.AnyFunSuite
class MainTest extends AnyFunSuite {
test("Example") {
assert(1 == 1)
}
}
On my build.sbt
file I have :
name := "my-scala-project"
version := "0.1"
scalaVersion := "3.0.0-M2"
libraryDependencies = "org.scalatest" %% "scalatest" % "1.4.RC2" % Test
I have also installed the library org.scalatest:scalatest_2.11:3.1.0
from Maven from the File > Project Structure... > Project Settings > Libraries window
On the editor I can see that Intellij is unable to resolve the import on my test file as it is marked in red. When I run sbt clean test
, I get this error message :
[error] -- [E006] Not Found Error: path\to\project\src\test\scala\MainTest.scala:4:2
[error] 4 | test("Example") {
[error] | ^^^^
[error] | Not found: test
[error] one error found
[error] one error found
[error] (Test / compileIncremental) Compilation failed
Why isn't it working ? I thought I have done everything as expected. Ideally I would like to use the latest 3.2.9 version of Scalatest.
Thanks
CodePudding user response:
You need to do two things to fix your code:
- Update
build.sbt
to use the correct dependency; and - Extend your test class with the necessary trait.
For 1:
Import the Scalatest library with the following:
libraryDependencies = "org.scalatest" %% "scalatest" % "3.2.10" % Test
When you use _2.11
in the current configuration you're asking SBT to find you version of scalatest for Scala 2.11, but you're using Scala 3.1.0. The %%
is a helper to pull in the correct version of Scalatest for your Scala version.
For 2:
Change your file as per the documention to:
class MainTest extends AnyFunSuite {
test("Example") {
assert(1 == 1)
}
}
CodePudding user response:
First of all, you have to change the scalaVersion
to 3.0.0-M2
, which is the latest supported.
As far as dependencies are concerned, as Jarrod has mentioned, you could either use
"org.scalatest" %% "scalatest" % "3.3.0-SNAP3" % Test
When using groupID %% artifactID % revision
you are telling sbt to fetch the artifactID suited for the scala version you have provided, in this case, 3.0.0-M2
.
You can also use groupID % artifactID % revision
. Like in your question, if scalatest_2.11
is the artifactID, it means scalatest
for scala version 2.11.x
will be downloaded.