简体   繁体   中英

Sbt not resolving Scalatest on Intellij IDEA

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

You need to do two things to fix your code:

  1. Update build.sbt to use the correct dependency; and
  2. 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)
  }
}

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.

I combine all the help I got in the comment:

I had to write this in the build.sbt file:

name := "meteo-france-client"

version := "0.1"

scalaVersion := "3.0.0-M2"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.3.0-SNAP3" % Test

Then sbt reload and sbt clean compile and it works. However I now get this exceptions:

[info] compiling 1 Scala source to C:\Users\adrie\OneDrive\Documents\dev\meteo-france-client\target\scala-3.0.0-M2\test-classes ...
[error] ## Exception when compiling 1 sources to C:\Users\adrie\OneDrive\Documents\dev\meteo-france-client\target\scala-3.0.0-M2\test-classes
[error] java.lang.NoClassDefFoundError: scala/collection/immutable/List
[error] xsbt.CachedCompilerImpl.run(CachedCompilerImpl.java:65)
[error] xsbt.CompilerInterface.run(CompilerInterface.java:41)
[error] java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
[error] java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] java.base/java.lang.reflect.Method.invoke(Method.java:567)
[error] sbt.internal.inc.AnalyzingCompiler.invoke(AnalyzingCompiler.scala:329)
[error] sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:112)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:192)
[error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
[error] sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:247)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:182)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:163)
[error] sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:163)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:210)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:175)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:173)
[error] sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:459)
[error] sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
[error] sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263)
[error] sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:414)
[error] sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:501)
[error] sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:401)
[error] sbt.internal.inc.Incremental$.apply(Incremental.scala:167)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
[error] sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
[error] sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
[error] sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:2357)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2314)
[error] sbt.internal.io.Retry$.apply(Retry.scala:46)
[error] sbt.internal.io.Retry$.apply(Retry.scala:28)
[error] sbt.internal.io.Retry$.apply(Retry.scala:23)
[error] sbt.internal.server.BspCompileTask$.compute(BspCompileTask.scala:31)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:2310)
[error] scala.Function1.$anonfun$compose$1(Function1.scala:49)
[error] sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
[error] sbt.std.Transform$$anon$4.work(Transform.scala:68)
[error] sbt.Execute.$anonfun$submit$2(Execute.scala:282)
[error] sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23)
[error] sbt.Execute.work(Execute.scala:291)
[error] sbt.Execute.$anonfun$submit$1(Execute.scala:282)
[error] sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
[error] sbt.CompletionService$$anon$2.call(CompletionService.scala:64)
[error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
[error] java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
[error] java.base/java.lang.Thread.run(Thread.java:831)
[error]
[error] stack trace is suppressed; run 'last Test / compileIncremental' for the full output
[error] (Test / compileIncremental) java.lang.NoClassDefFoundError: scala/collection/immutable/List
[error] Total time: 0 s, completed 26 nov. 2021, 13:33:34

I'll make another post about it: Sbt unable to find List class definition on Scala 3.0.0-M2

Thanks for all who have helped me here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM