ScalaTest Spec Compilation Error

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

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 

回答1:

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.



回答2:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!