ScalaTest Spec Compilation Error

2019-02-16 15:24发布

I am new to Scala and I am trying to use ScalaTest. I included its dependency in my build.sbt file as libraryDependencies++=Seq( "org.scalatest" % "scalatest_2.11" % "2.1.7" % "test" )

and refreshed sbt and now it appears in my External Libraries folder so I think it was installed correctly. Now I want to make a test class. So I created one under src/test/scala. I used the example from the ScalaTest website's frontpage which is

import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    }
  }
}

However when I run this class I get the error

 Error:scalac: bad symbolic reference. A signature in package.class refers to type compileTimeOnly
in package scala.annotation which is not available.
It may be completely missing from the current classpath, or the version on
the classpath might be incompatible with the version used when compiling package.class.

and also

 Error:(4, 27) Reference to class FlatSpec in package scalatest should not have survived past type checking,
it should have been processed and eliminated during expansion of an enclosing macro.
class ExampleSpec extends FlatSpec with Matchers {
                      ^

Could someone tell me what the problem is. It looks like it is not recognizing ScalaTest. However it is in my External Libraries and IntelliJ's auto-complete also shows that it is there. Do I somehow need to refresh something else before I can actually start using ScalaTest?

EDIT:

Also when I run test:compile from sbt I get

  [error] error while loading package, class file needed by package is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Matchers, class file needed by Matchers is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Assertions, class file needed by Assertions is missing.
[error] reference value internal of package scala.reflect.macros refers to nonexisting symbol.
[error] error while loading AbstractSuite, class file needed by AbstractSuite is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Tolerance, class file needed by Tolerance is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading BeWord, class file needed by BeWord is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading ResultOfNotWordForAny, class file needed by ResultOfNotWordForAny is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading NotWord, class file needed by NotWord is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] 8 errors found
[error] (test:compile) Compilation failed

2条回答
做自己的国王
2楼-- · 2019-02-16 15:44

It seems SBT tries to compile against scala 2.9.2. I guess you have to add one of

scalaVersion := "2.10.4"

or

scalaVersion := "2.11.1"

to your build.sbt

and of course

libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.7" % "test"

Additionally you can try the latest SBT launcher.

查看更多
做自己的国王
3楼-- · 2019-02-16 15:56

And if you need to compile with scala 2.9 then updated JAVA_HOME and IDEA_JDK to point to jdk1.7 instead of jdk1.8

查看更多
登录 后发表回答