【问题标题】:Test error while using ScalaTest使用 ScalaTest 时测试错误
【发布时间】:2017-05-01 03:29:30
【问题描述】:

我正在使用Scala 2.12.2ScalaTest 3.0.1sbt 0.13.15。我的 sbt 项目结构:

.
|-- build.sbt
|-- lib
|-- project
|-- src
|   |-- main
|   |   |-- java
|   |   |-- resources
|   |   |-- scala
|   |-- test
|       |-- java
|       |-- resources
|       |-- scala
              |-- ExampleSpec.scala
|-- target

我的build.sbt 文件:

name := "hello"

version := "1.0"

scalaVersion := "2.12.2"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

检查 scala 版本:

~$ scala -version
Scala code runner version 2.12.2 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.

ScalaTest Quick Start 挑选的ExampleSpec.scala 源代码:

import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
}

我现在的问题是当我尝试执行时:

~$ sbt
> test

我收到此错误消息:java.lang.NoSuchMethodError: scala.Predef$.refArrayOps

欢迎任何帮助。

【问题讨论】:

  • 我尝试完全按照您的步骤操作(创建build.sbtExampleSpec.scala 并在sbt 下运行test)并且无法重现该问题。你不会错过重要的事情吗?
  • 我检查了 sbt 目录,这是我得到的:imgur.com/a/BH4tL 请问这是 'scala-2.10.6' 这个错误的根源吗?
  • 我认为默认编译器 sbt 的目标是 scala-2.10。我也一样 (~/.sbt/boot/)

标签: scala sbt scalatest


【解决方案1】:

From latest scalatest document - ScalaTest 和 Scalactic 3.0.1 与 scala 2.10.0+2.11.0+2.12.0+ 一起使用

而且,安装了sbt-0.13.5 + scala 2.11.8 的版本对我来说非常完美。

$ /usr/local/sbt-0.13.5/bin/sbt clean test
[info] Set current project to hello (in build file:/Users/prayagupd/possibilities/deleteupd/)
[success] Total time: 0 s, completed Apr 29, 2017 5:06:47 AM
[info] Updating {file:/Users/prayagupd/possibilities/programming/s2/deleteupd/}deleteupd...
[info] Resolving jline#jline;2.14.3 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/prayagupd/possibilities/programming/deleteupd/target/scala-2.12/test-classes...
[info] 'compiler-interface' not yet compiled for Scala 2.12.2. Compiling...
[info]   Compilation completed in 9.129 s
[warn] there were two deprecation warnings (since 2.12.0); re-run with -deprecation for details
[warn] one warning found
[info] ExampleSpec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] Run completed in 276 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 12 s, completed Apr 29, 2017 5:06:59 AM

它也适用于sbt 0.13.15 + scala 2.12.2

/usr/local/sbt-0.13.15/bin/sbt clean test
[info] Loading project definition from /Users/prayagupd/possibilities/programming/deleteupd/project
[info] Set current project to whats-in-stream (in build file:/Users/prayagupd/possibilities/programming/deleteupd/)
[success] Total time: 0 s, completed Apr 30, 2017 12:01:33 PM
[info] Updating {file:/Users/prayagupd/possibilities/programming/deleteupd/}deleteupd...
[info] Resolving jline#jline;2.14.3 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/prayagupd/possibilities/programming/deleteupd/target/scala-2.12/test-classes...
[warn] there were two deprecation warnings (since 2.12.0); re-run with -deprecation for details
[warn] one warning found
[info] ExampleSpec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] Run completed in 211 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 5 s, completed Apr 30, 2017 12:01:37 PM

它也适用于 2.10 的 scalatest,安装的 scala 版本是2.12.2

name := "whats-in-stream"                                                                                                                                                       

version := "1.0"                                                                                       

//scalaVersion := "2.12.2"                                                                             

resolvers += "Artima Maven Repository" at "http://repo.artima.com/releases"                            

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"                                        

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test" 

// 运行测试

$ /usr/local/sbt-0.13.15/bin/sbt clean test
[info] Loading project definition from /Users/prayagupd/possibilities/programming/deleteupd/project
[info] Set current project to whats-in-stream (in build file:/Users/prayagupd/possibilities/programming/deleteupd/)
[success] Total time: 0 s, completed Apr 30, 2017 12:06:24 PM
[info] Updating {file:/Users/prayagupd/possibilities/programming/deleteupd/}deleteupd...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] downloading https://repo1.maven.org/maven2/org/scalactic/scalactic_2.10/3.0.1/scalactic_2.10-3.0.1.jar ...
[info]  [SUCCESSFUL ] org.scalactic#scalactic_2.10;3.0.1!scalactic_2.10.jar(bundle) (1628ms)
[info] downloading https://repo1.maven.org/maven2/org/scalatest/scalatest_2.10/3.0.1/scalatest_2.10-3.0.1.jar ...
[info]  [SUCCESSFUL ] org.scalatest#scalatest_2.10;3.0.1!scalatest_2.10.jar(bundle) (10607ms)
[info] Done updating.
[warn] Scala version was updated by one of library dependencies:
[warn]  * org.scala-lang:scala-library:2.10.4 -> 2.10.6
[warn] To force scalaVersion, add the following:
[warn]  ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
[warn] Run 'evicted' to see detailed eviction warnings
[info] Compiling 1 Scala source to /Users/prayagupd/possibilities/programming/deleteupd/target/scala-2.10/test-classes...
[info] ExampleSpec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] Run completed in 193 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 18 s, completed Apr 30, 2017 12:06:42 PM

根据您遇到的错误,它显然是版本不匹配。我建议清理依赖项并让 sbt 再次使用 sbt clean test 管理它。

最大规模的缓存将在,

$ ll ~/.ivy2/cache/org.scalatest
total 0
 6473425 drwxr-xr-x   9 prayagupd  N\Domain Users  306 Apr 30 12:06 scalatest_2.10
 5455138 drwxr-xr-x  21 prayagupd  N\Domain Users  714 Jan 29 19:44 scalatest_2.11
15453522 drwxr-xr-x   6 prayagupd  N\Domain Users  204 Apr 29 05:05 scalatest_2.12

【讨论】:

  • 由于兼容性要求,我必须使用 scala 2.12.x。 ScalaTest 网页明确指出:ScalaTest 和 Scalactic 3.0.1(2.10.0+、2.11.0+ 和 2.12.0+ 的最新版本)
  • 你是对的。我检查了最新的文档并检查了它是否适用于我的所有 scala 版本。 (更新的答案),另外你介意清理你的依赖然后做sbt clean test??
猜你喜欢
  • 2016-08-29
  • 2020-01-27
  • 2012-05-20
  • 1970-01-01
  • 2015-03-02
  • 2014-08-19
  • 2018-08-10
  • 2018-11-21
  • 1970-01-01
相关资源
最近更新 更多