0

I am using Scala 2.12.2, ScalaTest 3.0.1 and sbt 0.13.15. My sbt project structure :

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

My build.sbt file :

name := "hello"

version := "1.0"

scalaVersion := "2.12.2"

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

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

Checking for scala version :

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

ExampleSpec.scala source code picked from ScalaTest Quick Start :

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

My question now is when I try to execute :

~$ sbt
> test

I get this error message : java.lang.NoSuchMethodError: scala.Predef$.refArrayOps

Any help will be more than welcome.

User_X220
  • 53
  • 8
  • I tried to follow your steps exactly (created `build.sbt`, `ExampleSpec.scala` and run `test` under `sbt`) and can't reproduce the issue. Don't you missing something important? – SergGr Apr 30 '17 at 00:58
  • I have checked sbt directory and this what I get : http://imgur.com/a/BH4tL Is this 'scala-2.10.6' the origin of this error please? – User_X220 Apr 30 '17 at 11:49
  • I think default compiler sbt is targeted is scala-2.10. Its same with me (`~/.sbt/boot/`) – prayagupa Apr 30 '17 at 19:32
  • https://stackoverflow.com/questions/75947449/run-a-scala-code-jar-appear-nosuchmethoderrorscala-predef-refarrayops – Dmytro Mitin Apr 07 '23 at 05:04

1 Answers1

0

From latest scalatest document - ScalaTest and Scalactic 3.0.1 work with scala 2.10.0+, 2.11.0+, and 2.12.0+

And, its working perfect to me with sbt-0.13.5 + scala 2.11.8 installed version.

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

It also works with 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

It's also working with scalatest for 2.10, installed scala version is 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" 

// running tests

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

Based on the error you have, its clearly a version mismatch. I recommend to cleanup dependencies and let sbt manage it again with sbt clean test.

the scalatest cache would be at,

$ 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
prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • I have to use scala 2.12.x due to compatibility requirements. ScalaTest webpage states clearly that : ScalaTest and Scalactic 3.0.1 (the latest release for 2.10.0+, 2.11.0+, and 2.12.0+) – User_X220 Apr 30 '17 at 11:56
  • you are right. I checked latest doc and checked that its working for all scala versions to me. (updated answer), plus would you mind cleaning your dependencies and then do `sbt clean test`?? – prayagupa Apr 30 '17 at 19:20