Scalatest throws weird error on scala 2.11

1.3k views Asked by At

I'm using maven 3.3 + scala 2.10 for a small project. The scalatest framework works perfectly well. However, when I switch to scala 2.11 and replace all dependencies to 2.11:

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

The first time I ran mvn test it threw this error:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- scalatest-maven-plugin:1.0:test (test) @ spookystuff-core ---
An exception or error caused a run to abort. This may have been caused by a problematic custom reporter.
java.lang.NoSuchMethodError: scala.runtime.ObjectRef.create(Ljava/lang/Object;)Lscala/runtime/ObjectRef;
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2347)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1044)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1043)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2722)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1043)
    at org.scalatest.tools.Runner$.main(Runner.scala:860)
    at org.scalatest.tools.Runner.main(Runner.scala)

Can someone explain what does it means, and how to fix it?

3

There are 3 answers

2
tribbloid On BEST ANSWER

OK I found the problem. Doing a mvn dependency:tree shows that a library doesn't ship with scala 2.11, when compiling its dependency still resolves to 2.10.

The solution is to exclude its dependency:

    <dependency>
        <groupId>com.github.mdr</groupId>
        <artifactId>ascii-graphs_2.10</artifactId>
        <version>0.0.6</version>
        <exclusions>
            <exclusion>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

The everything becomes normal.

2
Alexey Romanov On

If you are using Maven with Scala, don't write scalatest_2.11; instead, use something like

<properties>
  <encoding>UTF-8</encoding>
  <scala.binary.version>2.11</scala.binary.version>
</properties>
...
  <artifactId>scalatest_${scala.binary.version}</artifactId>

Otherwise, you'll encounter issues like this regularly.

0
NGUYEN Anh On

I used maven 3.x.x and scala 2.11.12

Solution worked for me, hope it work on you too:

you need to add theses dependencies to your block into your pom.xml

<dependency>
  <groupId>org.scala-lang</groupId>
  <artifactId>scala-library</artifactId>
  <version>2.11.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.scalatest/scalatest -->
<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_2.11</artifactId>
  <version>3.2.0-SNAP10</version>
  <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.scalacheck/scalacheck -->
<dependency>
  <groupId>org.scalacheck</groupId>
  <artifactId>scalacheck_2.11</artifactId>
  <version>1.15.1</version>
  <scope>test</scope>
</dependency>

And disable surefire and enable scalatest by adding theses plugins into block

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.4.0</version>
    <configuration>
      <scalaVersion>2.11.12</scalaVersion>
    </configuration>
  </plugin>
  <!-- disable surefire -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.7</version>
    <configuration>
      <skipTests>true</skipTests>
    </configuration>
  </plugin>
  <!-- enable scalatest -->
  <plugin>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest-maven-plugin</artifactId>
    <version>1.0</version>
    <configuration>
      <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
      <junitxml>.</junitxml>
      <filereports>WDF TestSuite.txt</filereports>
    </configuration>
    <executions>
      <execution>
        <id>test</id>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>