json4s提取默认值

时间:2019-04-25 01:58:50

标签: scala json4s

这是我的测试用例

case class WithDefaultValueHolder(values: Seq[WithDefaultValue])
case class WithDefaultValue(name: String, gender: String = "male")

it("default with valid json"){
  val res = WithDefaultValueHolder(Seq(WithDefaultValue("Bob")))
  parse("""{"values":[{"name":"Bob","gender":"male"}]}""").extract[WithDefaultValueHolder](
    DefaultFormats, Manifest.classType(classOf[WithDefaultValueHolder])) shouldBe (res)
}

下面的测试也通过了,它认为格式错误的json是默认值的有效json。为什么?如何限制这种行为。如何在测试用例下失败?

it("default with invalid json"){
  val res = WithDefaultValueHolder(Seq(WithDefaultValue("Bob")))
  parse("""{"values":[{"name":"Bob","xyz":"abc"}]}""").extract[WithDefaultValueHolder](
    DefaultFormats, Manifest.classType(classOf[WithDefaultValueHolder])) shouldBe (res)
}

0 个答案:

没有答案