1

我的 ScalaTest 在尝试将 JSON 字符串解析为自定义 Scala 对象时中断。我正在使用 Play-Json 库进行 [反] 序列化。序列化工作正常,但在 Blah 类上运行单元测试时反序列化中断。该测试调用 fromJsonString() 方法和 Im 使用 ScalaTest 库进行单元测试。在这里感谢一些帮助。

异常跟踪:(完整跟踪 - http://pasted.co/e627b1ee

An exception or error caused a run to abort: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object; 
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
    at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:144)
    at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:108)
    at play.api.libs.json.jackson.JsValueDeserializer.deserialize(JacksonJson.scala:103)
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3536)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1872)
    at play.api.libs.json.jackson.JacksonJson$.parseJsValue(JacksonJson.scala:226)
    at play.api.libs.json.Json$.parse(Json.scala:21)
    at com.project.gateway.model.Blah.fromJsonString(Blah.scala:98)

Scala 对象定义:

case class Blah(name: String, id: String) {

      implicit val BlahWrites: Writes[Blah] = (
        (JsPath \ "name").write[String] and
          (JsPath \ "id").write[String]
        )(unlift(Blah.unapply))

      implicit val BlahReads: Reads[Blah] = (
        (JsPath \ "name").read[String] and
          (JsPath \ "id").read[String]
        )(Blah)


      def toJsonString(): String = {
        Json.toJson(this).toString()
      }

      def fromJsonString(jsonString: String): Blah = {
        val value = Json.parse(jsonString)
        value.as[Blah]
        //Json.fromJson[Blah](value).get
      }
    }

我的 SBT 文件:

name := "Project"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies += "com.typesafe.scala-logging" % "scala-logging_2.11" % "3.1.0"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
libraryDependencies += "com.typesafe.play" % "play-json_2.10" % "2.4.2"
4

1 回答 1

2

我猜想在运行时/测试时的 scala-library 版本与用于构建引发错误的依赖项的版本之间存在差异。

如果使用 SBT 或 Maven,您可以检查使用的库(包括传递库),因此检查 scala-library 是否不兼容(通过不同的依赖项拉入不同的版本)。

于 2015-09-16T08:16:27.473 回答