Forum Discussion

jfesri's avatar
jfesri
Occasional Contributor
2 years ago

How to check for the existence of a value in Groovy script

I have an existing Groovy script (for an assertion) that I need to change to check whether a variable exists, but not caring about the actual value. The script is using results from the test step.

 

def expectedMsgs = ["""
{"msgType":"Message","message":{"variable1":"<value>","variable2":[{"variable2a":"<value>","variable2b":"<value>","variable2c":"<value>","variable2d":"<value>"}]}}

 

def msg = context.receivedMessage

 

def isMatch = expectedMsgs.stream().any { em -> org.skyscreamer.jsonassert.JSONCompare.compareJSON(em, msg, org.skyscreamer.jsonassert.JSONCompareMode.LENIENT).passed()}
log.info ("Websocket message #${context.messagetCount} ${isMatch ? "matched" : "did not match"} the list of expected string")

 

assert(isMatch)

 

Some of these values I do want to check an exact response is received and that's easy enough. But some of them, I just want to make sure the variable itself is present and value isn't null. Can't sort out how to handle that part.

 

Thanks!

Jonathan