简体   繁体   中英

Scala 2.11 -> 2.12 — Unit test fails in sbt, works in IntelliJ

I have a project that has been working under Scala 2.11, and I'm in the process of upgrading it to use Scala 2.12. I've got a unit test that fails when I run it on the command line through sbt, but it works fine when I run it through IntelliJ IDEA.

The error I get is:

java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V

The test is a JUnit test in Java, and it invokes code written in Scala.

The offending line in the unit test:

String paramsJson = adapter.sendReadyMessage();

The sendReadyMessage method (also Java) attempts to create a ReadyMessage :

ReadyMessage rm = new ReadyMessage(timestamp);

And a ReadyMessage is just a Scala case class

case class ReadyMessage(Timestamp: String) extends ToJson

And ToJson is a lift-JSON helper trait that encapsulates JSON generation:

import net.liftweb.json.{DefaultFormats, Serialization}

trait ToJson {
  private implicit val formats = DefaultFormats
  def toJson = Serialization.write(this)
}

The error appears to happen on the case class ReadyMessage line. The ToJson block is there for context.

Like I said, it works when I run it from within IntelliJ. It fails when I run it under sbt. It fails when it's part of the whole batch of tests in sbt, and it fails when I run it on its own in sbt.

Short of pasting my entire build.sbt file, I'm not sure what other information to include here that might be useful. It seems like it's some kind of version conflict, but I went through and updated all of the software version numbers I can find.

I realize this question covers an issue that may be more complex than can be reasonably debugged over SO. But even a "make sure you've updated XX in your build.sbt" response is helpful.

The answer turned out to be as annoying as the problem itself.

After many hours of searching my build, I realized that there was a "lib" directory in the subproject that was failing. I opened it up, and sure enough, it contained old versions of Scala and some other libs. I deleted those, made sure the libraryDependencies took care of them, and now everything builds and tests just fine.

Thanks to everyone for keeping me on board the "it's a Scala version mismatch" train. And let this be a cautionary tale, kids. Mix your managed and unmanaged dependencies at your own peril.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM