Forum Discussion

codehausss's avatar
codehausss
Contributor
9 years ago
Solved

Groovy for assertion not contain and json path

Hi folks   is it possible to assert not contains and json path using groovy script? i know in the test step soapUI can add assertion via UI for contain, not contain, json path, etc.   how to do ...
  • rupert_anderson's avatar
    9 years ago

    Hi,

     

    Yes, consider the example:

     

     

     

    import static com.jayway.jsonpath.JsonPath.parse
    
    def json = '''{"quote": {
    	"id": "test_id",
    	"amount": 100,
    	"links":    [
    			 {
    		  "rel": "self",
    		  "href": "http://localhost:8080/quote/777"
    	   },
    			 {
    		  "rel": "customer",
    		  "href": "http://localhost:8080/customer/12345"
    	   }
    	]
     }}
    }'''
    
    def quoteId = parse(json).read('$.quote.id')
    log.info quoteId
    
    assert quoteId=="test_id"
    assert quoteId.contains('test_id')
    assert !quoteId.contains('testid') //not contains

    In the case  of a script Assertion, simply replace the json string with the script assertion's property 

    messageExchange.response.contentAsString

    Is this what you wanted?

     

    Cheers,

    Rupert