Scala 检查JsonPath中的空对象
我试图验证一些用于Gatling模拟的JsonPath对象,它对非null对象运行良好,但对“null”对象失败 实际上字符串“null”和对象null比较失败,我如何处理这种情况 我们正在检查错误,如下所示:Scala 检查JsonPath中的空对象,scala,gatling,jsonpath,Scala,Gatling,Jsonpath,我试图验证一些用于Gatling模拟的JsonPath对象,它对非null对象运行良好,但对“null”对象失败 实际上字符串“null”和对象null比较失败,我如何处理这种情况 我们正在检查错误,如下所示: .check(jsonPath("$.userId").ofType[String].is("null")) 或 但是,获取错误作为 failed: jsonPath($.userId).find.is(null), but actually found null 运气好吗看来加特林
.check(jsonPath("$.userId").ofType[String].is("null"))
或
但是,获取错误作为
failed: jsonPath($.userId).find.is(null), but actually found null
运气好吗看来加特林支票系统没有很好地处理
null
;假设所有东西都使用选项
,但JSON处理不是这样工作的
不过,您可以使用更通用的validate()
方法来解决此问题。首先定义一个简单的验证器:
def notNull[T] = new Validator[T] {
val name = "notNull"
def apply(actual : Option[T]) : Validation[Option[T]] = {
actual match {
case Some(null) => Failure("but it's so null you guys")
case _ => Success(actual)
}
}
}
然后:
比我的第一个更好的答案是:
jsonPath("$.userId").ofType[Option[String]].not(None)
毕竟,这是Scala;我们不喜欢这里的null
不幸的是,这不起作用,因为Gatling缺少选项的JsonFilter
。不过写起来并不难:
implicit def optionJsonFilter[T : JsonFilter] : JsonFilter[Option[T]] = {
new JsonFilter[Option[T]] {
def filter = {
case null => None
case other => {
val subfilter : JsonFilter[T] = implicitly
Some(subfilter.filter(other))
}
}
}
}
(如果是未来,加特林已经解决了这个问题,请编辑它。)您可以使用原始的加特林DSL API进行null
检查:
check(jsonPath("$.userId").notNull)
有关更多详细信息,请参见Thank@Luke,请为Gatling撰写一份公关报告。谢谢,我非常感谢您的努力_/_
implicit def optionJsonFilter[T : JsonFilter] : JsonFilter[Option[T]] = {
new JsonFilter[Option[T]] {
def filter = {
case null => None
case other => {
val subfilter : JsonFilter[T] = implicitly
Some(subfilter.filter(other))
}
}
}
}
check(jsonPath("$.userId").notNull)