使用ScalaTest时测试错误

时间:2017-04-29 11:24:58

标签: scala sbt scalatest

我正在使用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

任何帮助都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

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

它还使用scalatest为2.10,安装的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 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