Scalatest 在 Scala 2.11 上抛出奇怪的错误

Scalatest throws weird error on scala 2.11

我在一个小项目中使用 maven 3.3 + scala 2.10。 scalatest 框架运行良好。但是,当我切换到 scala 2.11 并将所有依赖项替换为 2.11 时:

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

我第一次 运行 mvn 测试时抛出了这个错误:

-------------------------------------------------------
 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.apply(Runner.scala:1044)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter.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)

谁能解释一下这是什么意思,以及如何解决它?

好的,我找到问题了。执行 mvn dependency:tree 显示库未随 scala 2.11 一起提供,编译时其依赖项仍解析为 2.10.

解决办法是排除它的依赖:

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

一切都正常了。

如果您在 Scala 中使用 Maven,不要scalatest_2.11;相反,使用类似

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

否则,您会经常遇到这样的问题。

我用的是 maven 3.x.x 和 scala 2.11.12

解决方案对我有用,希望对你也有用:

您需要将这些依赖项添加到您的 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>

并通过将这些插件添加到块中来禁用 surefire 并启用 scalatest

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