generall
generall

Reputation: 348

Error on scalatest compilation in IDEA

I am trying to compile Scala project which contains scalatest. It compiles normal on sbt

sbt
> compile
> test:compile

, but when I am trying to build it with IDEA, it shows the following error:

Error:(37, 11) exception during macro expansion: 
java.lang.NoSuchMethodError: org.scalactic.BooleanMacro.genMacro(Lscala/reflect/api/Exprs$Expr;Ljava/lang/String;Lscala/reflect/api/Exprs$Expr;)Lscala/reflect/api/Exprs$Expr;
at org.scalatest.AssertionsMacro$.assert(AssertionsMacro.scala:34)
assert((ElementMeasures.baseElementDistance(mEl1, mEl2) - 0.33333).abs < 0.001)
      ^

for each assert function in test.

build.sbt file contains following:

name := "ner-scala"
organization := "ml.generall"
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.8"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"
...

Upvotes: 21

Views: 11126

Answers (7)

Andrew Norman
Andrew Norman

Reputation: 911

over the years the MockitoSugar trait for scalatest was moved from scalatest to mockito.

In my situation I had this error where I had a multiple inheritance issue where my test class was directly using org.mockito.scalatest.MockitoSugar and another extended trait was using the older org.scalatest.mockito.MockitoSugar

If you replace all the exiting org.scalatest.mockito.MockitoSugar imports with org.mockito.scalatest.MockitoSugar then this issue goes away.

Upvotes: 0

Tomer Shetah
Tomer Shetah

Reputation: 8539

Sometimes, there are incompatibilities between how Intellij IDEA compiles scala, versus how sbt compiles scala. What you can try doing, is ask the IDEA to compile as sbt, and not as it does by default.

In the sbt view, you need to open the sbt settings. enter image description here

Once it is opened you can enable the "builds" checkbox. As stated by IDEA, it is recomended for sbt build configurations that cause compilation nin IDEA to not work correctly. enter image description here

Upvotes: 1

robert
robert

Reputation: 1532

I had a similar issue, but excluding org.scalatest did not fix it.

Instead, I excluded org.scalactic:

"com.stackoverflow" %% "troublesome-library" % "1.0.420" excludeAll(ExclusionRule(organization="org.scalactic"))

Upvotes: 0

Harsh Patil
Harsh Patil

Reputation: 1

Excluding just the org.scalatest group did not work for me. On analyzing my mvn dependencies I had to exclude the scalactic_{scala_binary_version} group as well.

<exclusion>
  <artifactId>scalactic_2.11</artifactId>
  <groupId>org.scalactic</groupId>
</exclusion>

Upvotes: 0

medale
medale

Reputation: 326

I just ran into the same problem and as Alexey described (he should get the upvote but I don't have enough reputation to upvote or comment - thank you Alexey), it seems that it was caused by having multiple versions of scalatest in my project. I was able to fix it by specifically excluding the older scalatest from the library that brought it in (and please note that the exclude needs to specify the scala binary version, e.g. _2.11 etc.!):

...exclude("org.scalatest", "scalatest_2.11")

There had also been a warning in the event log before the exclude:

SBT project import
[warn] Multiple dependencies with the same organization/name but different versions.
[warn]  * org.scalatest:scalatest_2.11:(2.2.6, 3.0.1)

Upvotes: 16

Alexey
Alexey

Reputation: 2488

It may also mean that you have more than one versions of scalatest registered. I came into pretty same issue with compile-time error on assert

Upvotes: 22

keypoint
keypoint

Reputation: 2318

I think your IntelliJ is missing library scalatest

from IntelliJ, go to Project Structure -> Project Settings -> Libraries -> + symbol -> From Maven -> search for scalatest with correct version

after adding scalatest library for IntelliJ, assert error should disappear.

This is not a guaranteed solution, just give it a try :)

Upvotes: 1

Related Questions