Groovy - Asserting that all JSON response elements have content
I have a test scenario with hundreds of response elements and I am trying to script an assertion to ensure that none of the elements returned as null. There must be an easy way to use a wildcard to search the entire response body for a null field, but I can't figure out how to do it. Can anyone suggest a better solution to ensure no nulls were returned?
Response snippet:
{
"errors" : [
],
"transactionId" : "123456789",
"dateTime" : "2017-12-08T22:07:43.099Z",
"observed" : {
"date" : "2017-12-08",
"obs" : 39,
"conditions" : {
"text" : "Clear",
"code" : 85
}
},
etc.....
And the code I have so far:
import com.parasoft.api.*
import groovy.json.JsonSlurper
import java.util.*
import soaptest.api.*
void extractAndAssertResponse(Map input, ScriptingContext context) {
def slurper = new JsonSlurper()
String responseBody = input.get(SOAPUtil.XML_RESPONSE)
Application.showMessage("RESPONSE_BODY: " + responseBody)
def json = slurper.parseText(responseBody) assert json.transactionId != null assert json.dateTime != null assert json.observed.date != null
}
Comments
-
Hello Gambit,
If you don't expect "null" to be anywhere in your payload, it may be easier to use the "Search Tool" to throw an error if the particular string is found. For example:
0 -
Hi OmarR,
The search tool looks like it would work fine for the literal "null" field value, but how would I account for the scenario of a return of ""?
0 -
You could add
""
as one of the search terms in the tool.
For example:
The Json Assertor could also accomplish the same for specific elements in the payload:
Give these tools a try and let us know if you have any questions about them1 -
Hi Omar,
I've tested this solution and it works for my problem. Thanks!
0