Error on scalatest compilation in IDEA
Asked Answered
C

7

21

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"
...
Calceiform answered 1/10, 2016 at 22:27 Comment(3)
have checked the sdk IDEA used?Gynecic
Yes. scala-sdk-2.11.8 seems ok.Calceiform
Did the problem persist? What version of IDEA and Scala plugin are you using?Louralourdes
B
22

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

Blowy answered 23/11, 2016 at 10:54 Comment(2)
Used dependencyOverrides += "org.scalatest" %% "scalatest" % "3.0.1" to fix this. It was strange: my sbt build worked, but the IDEA build failed - even though I had opened the project in IDEA as an sbt project.Nonsuit
Just jumping in to add that this can also happen when you have more than one version of Scala registered. Using Gradle, I had to tell my project to exclude org.scala-lang:scala-library from being pulled in from a downstream project.Rufina
M
16

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)
Mover answered 27/11, 2016 at 1:8 Comment(0)
B
1

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 :)

Bow answered 3/10, 2016 at 17:37 Comment(0)
S
1

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

Subvert answered 31/7, 2020 at 9:56 Comment(0)
O
0

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>
Objectify answered 17/4, 2020 at 18:18 Comment(0)
W
0

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"))
Wheaten answered 30/7, 2020 at 22:30 Comment(1)
In my case, excluding both organization names still didn't avoid the NoSuchMethodErrorBreastfeed
M
0

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.

Maupin answered 13/12, 2023 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.