Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScalaTest Spec Compilation Error

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
like image 708
user1893354 Avatar asked May 31 '14 22:05

user1893354


People also ask

Should VS must ScalaTest?

should and must are the same semantically. But it's not about better documentation, it's basically just down to personal stylistic preference (I prefer must for example).

How do you fail a Scala test?

Or, if you want the test to fail with a message, write: fail("I've got a bad feeling about this.")

What is AnyFlatSpec?

class AnyFlatSpec extends AnyFlatSpecLikeFacilitates a “behavior-driven” style of development (BDD), in which tests are combined with text that specifies the behavior the tests verify.

What is FunSuite in Scala?

A suite of tests in which each test is represented as a function value. The “ Fun ” in FunSuite stands for “function.” Here's an example FunSuite : import org.scalatest.FunSuite.


1 Answers

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.

like image 174
Klaus Avatar answered Oct 07 '22 08:10

Klaus