Forum Discussion

performerj's avatar
Occasional Contributor
8 years ago

Reusable assertions for REST error handling in SoapUI

I've been asked to come up with a way to provide reusable assertions for validating standard error responses (HTTP Status Codes 400, 403, etc.) so that those who develop other SoapUI TestCases that e...
  • performerj's avatar
    8 years ago

    I came up with the following which works at the script assertion level.


    // Logic for comparing response to JSON schema in Groovy
    import groovy.json.JsonSlurper
    def responseStatus = messageExchange.responseHeaders['#status#'][0]
    if ( responseStatus != 200 ) {  // Error code, make sure we got the confirmation message. 
      URL schemaUrl = new URL("")  // Create URL object in Groovy.
      def jsonSchemaMap = new JsonSlurper().parseText(schemaUrl.text) // Slurp JSON schema data into a Groovy map.
      def ResponseMessage = messageExchange.response.responseContent   // Fetch the JSON payload from the response message.
      def jsonResponseMap = new JsonSlurper().parseText(ResponseMessage)  // Slurp JSON response data into a Groovy map.
      def schemaKeys = jsonResponseMap.keySet() // Get the list of keys from the JSON schema.
      def responseKeys = jsonResponseMap.keySet()// Get the list of keys from the JSON response.
      assert schemaKeys == responseKeys  // Assert that the key lists from the JSON schema and the JSON response should match.
                                         // This is our indicator that we got a valid confirmation message.
    else assert responseStatus == 200    // Otherwise, assert we got the normal expected response (HTTP Status Code = 200).

    From here I think it is mainly a matter of packaging things with classes, etc. to prepare for reuse from a script library.