使用ScalaTest时出现测试错误
我使用的是使用ScalaTest时出现测试错误,scala,sbt,scalatest,Scala,Sbt,Scalatest,我使用的是scala2.12.2,scalatest3.0.1和sbt0.13.15。我的sbt项目结构: . |-- build.sbt |-- lib |-- project |-- src | |-- main | | |-- java | | |-- resources | | |-- scala | |-- test | |-- java | |-- resources | |-- scala
scala2.12.2
,scalatest3.0.1
和sbt0.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.
ExampleSpec.scala源代码选自:
我现在的问题是,当我试图执行:
~$ sbt
> test
我收到这个错误消息:java.lang.NoSuchMethodError:scala.Predef$.refArrayOps
欢迎提供任何帮助。-ScalaTest和Scalactic 3.0.1使用scala2.10.0+
、2.11.0+
和2.12.0+
而且,在安装了sbt-0.13.5
+scala2.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
它也适用于sbt0.13.15
+scala2.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
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"
它还与scalatest for 2.10配合使用,安装的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
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
我试图完全按照您的步骤(创建
build.sbt
,ExampleSpec.scala
并在sbt
下运行test
),但无法重现问题。你没有遗漏一些重要的东西吗?我已经检查了sbt目录,我得到的结果是:这是“scala-2.10.6”吗?我想默认编译器sbt的目标是scala-2.10。我也一样(~/.sbt/boot/
),由于兼容性要求,我不得不使用scala 2.12.x。ScalaTest网页明确指出:ScalaTest和Scalatic 3.0.1(2.10.0+、2.11.0+和2.12.0+的最新版本)你是对的。我检查了最新的文档,并向我检查了它是否适用于所有scala版本。(更新的答案),另外,您是否介意清理依赖项,然后执行sbt clean测试
??