How to assert JSON not null object

3,574 views
Skip to first unread message

Materia Confusa

unread,
Jul 2, 2014, 1:59:36 PM7/2/14
to rest-a...@googlegroups.com
Must be missing something really easy. Say I have a JSON response like so:

{
    "status_code": 200,
    "response_size": "144",
    "data": null,
    "errors": [
        {
            "message": "Something............"
        }
    ]
}

I need to assert that data is not a null object. There is a notNullValue() but that isn't correct. What am I missing?

TIA

Johan Haleby

unread,
Jul 3, 2014, 12:29:15 AM7/3/14
to rest-a...@googlegroups.com
You can indeed use notNullValue(). If you have a service called "x" that returns your JSON you can do like this:

when().
          get("/x").
then().
         body("data", notNullValue());

/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Materia Confusa

unread,
Jul 3, 2014, 1:44:04 PM7/3/14
to rest-a...@googlegroups.com
Ah, I see what I was doing wrong. Seems you can't do it all in one body() call. This will not work:

body("data", notNullValue(), "data.publisher_id", equalTo("whatever"))

Throws:
java.lang.IllegalArgumentException: Cannot get property 'publisher_id' on null object

Johan Haleby

unread,
Jul 3, 2014, 3:07:57 PM7/3/14
to rest-a...@googlegroups.com
Hmm I actually think that should work as well. But if data is null then perhaps you can run into that exception.
Reply all
Reply to author
Forward
0 new messages