【问题标题】:Scalatest throws weird error on scala 2.11Scalatest 在 scala 2.11 上抛出奇怪的错误
【发布时间】:2016-12-28 20:49:05
【问题描述】:

我在一个小项目中使用 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 test 时抛出了这个错误:

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

有人能解释一下这是什么意思,以及如何解决吗?

【问题讨论】:

  • 在 Scala 中,所有对象都必须使用相同的 Scala 版本进行编译,因此使用每个库的正确版本至关重要。

标签: scala scalatest scala-2.11 scalatest-maven-plugin


【解决方案1】:

如果你使用 Maven 和 Scala,不要scalatest_2.11;相反,使用类似的东西

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

否则,您会经常遇到此类问题。

【讨论】:

  • 非常感谢 Alex,这正是我所做的,但我使用的一个库没有 2.11 版本,所以我使用 2.10 版本,虽然它在 maven 中会被大部分 2.11 依赖项遮蔽,但事实证明阴影不能那样工作:)
  • “阴影不能那样工作”更少,而 Scala 不能那样工作。
【解决方案2】:

我使用了 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>

【讨论】:

    【解决方案3】:

    好的,我发现了问题。做一个 mvn 依赖:树表明一个库没有随 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>
    

    一切都变得正常了。

    【讨论】:

    • _2.10 末尾的ascii-graphs_2.10 表示这个依赖是为Scala 2.10 编译的
    • 我知道,但我天真地以为它会被遮蔽,就像许多潜在的罐子地狱一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多