user9016830
user9016830

Reputation: 11

How can I verify in groovy assert either value is a string or null

I have one response object of a REST call which is having JSON data. I am using groovy to verify one key value either it should be a string or null

For example

assert response.data.item.key

So key can be a string or a null. How can I put assertion on this key value?

Upvotes: 1

Views: 1154

Answers (2)

tim_yates
tim_yates

Reputation: 171194

Are you sure you want only a String?

If you use a groovy string or some other char sequence checking its class equals String will fail

def key = response.data.item.key
assert key == null || key instanceof CharSequence

May be better...

Upvotes: 1

injecteer
injecteer

Reputation: 20707

Assert could look like:

def key = response.data.item.key
assert null == key || String == key.getClass()

Upvotes: 1

Related Questions