Why won't my scalatest test compile? (scala.MatchError)

4k views Asked by At

There is all of the code in my project:

package fileSearcher

import org.scalatest.FlatSpec

class FilterCheckerTests extends org.scalatest.FlatSpec {
  "Foo" should
  "not do terrible things" in {

    assert(1 == 1)
  }  
}

Eclipse

sbt test crashes with scala.MatchError (full details below).

What am I doing wrong?

[info] Compiling 1 Scala source to C:\scala\course\FileSearcher\target\scala-2.1
0\test-classes...
[error]
[error]      while compiling: C:\scala\course\FileSearcher\src\test\scala\fileSe
archer\FilterCheckerTests.scala
[error]         during phase: typer
[error]      library version: version 2.10.4
[error]     compiler version: version 2.10.4
[error]   reconstructed args: -classpath C:\scala\course\FileSearcher\target\sca
la-2.10\test-classes;C:\scala\course\FileSearcher\target\scala-2.10\classes;C:\U
sers\Max\.ivy2\cache\org.scalatest\scalatest_2.11\bundles\scalatest_2.11-2.2.4.j
ar;C:\Users\Max\.ivy2\cache\org.scala-lang\scala-reflect\jars\scala-reflect-2.11
.2.jar;C:\Users\Max\.ivy2\cache\org.scala-lang.modules\scala-xml_2.11\bundles\sc
ala-xml_2.11-1.0.2.jar;C:\Users\Max\.ivy2\cache\com.novocode\junit-interface\jar
s\junit-interface-0.11.jar;C:\Users\Max\.ivy2\cache\junit\junit\jars\junit-4.11.
jar;C:\Users\Max\.ivy2\cache\org.hamcrest\hamcrest-core\jars\hamcrest-core-1.3.j
ar;C:\Users\Max\.ivy2\cache\org.scala-sbt\test-interface\jars\test-interface-1.0
.jar -bootclasspath C:\Program Files\Java\jdk1.8.0_20\jre\lib\resources.jar;C:\P
rogram Files\Java\jdk1.8.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_20\j
re\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jsse.jar;C:\Prog
ram Files\Java\jdk1.8.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_20\jre
\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfr.jar;C:\Program F
iles\Java\jdk1.8.0_20\jre\classes;C:\Users\Max\.ivy2\cache\org.scala-lang\scala-
library\jars\scala-library-2.10.4.jar
[error]
[error]   last tree to typer: Literal(Constant(true))
[error]               symbol: null
[error]    symbol definition: null
[error]                  tpe: Boolean(true)
[error]        symbol owners:
[error]       context owners: value <local FilterCheckerTests> -> class FilterCh
eckerTests -> package fileSearcher
[error]
[error] == Enclosing template or block ==
[error]
[error] Template( // val <local FilterCheckerTests>: <notype> in class FilterChe
ckerTests
[error]   "org.scalatest.FlatSpec" // parents
[error]   ValDef(
[error]     private
[error]     "_"
[error]     <tpt>
[error]     <empty>
[error]   )
[error]   // 2 statements
[error]   DefDef( // def <init>(): fileSearcher.FilterCheckerTests in class Filt
erCheckerTests
[error]     <method>
[error]     "<init>"
[error]     []
[error]     List(Nil)
[error]     <tpt> // tree.tpe=fileSearcher.FilterCheckerTests
[error]     Block( // tree.tpe=Unit
[error]       Apply( // def <init>(): org.scalatest.FlatSpec in class FlatSpec,
tree.tpe=org.scalatest.FlatSpec
[error]         FilterCheckerTests.super."<init>" // def <init>(): org.scalatest
.FlatSpec in class FlatSpec, tree.tpe=()org.scalatest.FlatSpec
[error]         Nil
[error]       )
[error]       ()
[error]     )
[error]   )
[error]   Apply(
[error]     "Foo".should("not do terrible things")."in"
[error]     Apply(
[error]       "assert"
[error]       Apply( // def ==(x: Int): Boolean in class Int, tree.tpe=Boolean(t
rue)
[error]         1."$eq$eq" // def ==(x: Int): Boolean in class Int, tree.tpe=(x:
 Int)Boolean
[error]         1
[error]       )
[error]     )
[error]   )
[error] )
[error]
[error] == Expanded type of tree ==
[error]
[error] ConstantType(value = Constant(true))
[error]
[error] uncaught exception during compilation: scala.MatchError
[trace] Stack trace suppressed: run last test:compile for the full output.
[error] (test:compile) scala.MatchError: false (of class scala.reflect.internal.
Trees$Literal)
[error] Total time: 0 s, completed Jun 20, 2015 11:07:15 AM
1. Waiting for source changes... (press enter to interrupt)
2

There are 2 answers

0
kiritsuku On BEST ANSWER

As you can see by looking at the classpath, which is printed by the compiler, you mixed Scala 2.10 with libraries for 2.11. Given that major versions of Scala are binary incompatible, this can never work.

This can be fixed with scalaVersion := "2.11.5" or by setting all dependencies to use the 2.10 versions, which can be done with libraryDependencies += "group" %% "libName" % "version", where %% means that sbt automatically uses the correct library version.

0
houcros On

For anyone getting a similar error (as I did) running Scala 2.10 using Maven instead of sbt, the solution would simply be to change the Maven dependency suggested in the ScalaTest website from

<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_2.11</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>

to

<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_2.10</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>