Search code examples
scalagatlingjsonpath

Checking null objects in JsonPath


I'm trying to validate some JsonPath objects for Gatling simulations and it's working fine for non-null objects but it's failing for "null" objects.

Actually the String "null" and object null comparision get failed, how can I handle this situation ?

We are checking error as below,

.check(jsonPath("$.userId").ofType[String].is("null"))

OR

.check(jsonPath("$.userId").ofType[Any].is(null))

But, get error as

failed: jsonPath($.userId).find.is(null), but actually found null

any luck


Solution

  • A better answer than my first one would be:

    jsonPath("$.userId").ofType[Option[String]].not(None)
    

    After all, this is Scala; we don't take kindly to null around here.

    Unfortunately, this doesn't work, because Gatling lacks a JsonFilter for Option. Not hard to write, though:

    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))
          }
        }
      }
    }
    

    (If it's the future and Gatling has fixed this, please do edit this.)