我试图验证一些JasonPath对象以进行Gatling模拟,并且它对非空对象起作用,但是它失败了" null"对象。
实际上String" null"和对象null比较失败,我该如何处理这种情况?
我们正在检查错误如下,
.check(jsonPath("$.userId").ofType[String].is("null"))
OR
.check(jsonPath("$.userId").ofType[Any].is(null))
但是,得到错误
failed: jsonPath($.userId).find.is(null), but actually found null
任何运气
答案 0 :(得分:1)
比我的第一个更好的答案是:
jsonPath("$.userId").ofType[Option[String]].not(None)
毕竟,这是Scala;我们不要在这附近null
采取行动。
不幸的是,这不起作用,因为Gatling缺少JsonFilter
Option
。不难写,但是:
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))
}
}
}
}
(如果是未来,加特林已经解决了这个问题,请对此进行编辑。)
答案 1 :(得分:0)
Gatling检查系统似乎没有设置好处理null
;它假设所有内容都使用Option
,但这并不是它的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)
}
}
}
然后:
.check(jsonPath("$.userId")).validate(notNull[String])
答案 2 :(得分:0)