Ask a Question

Groovy for assertion not contain and json path

SOLVED
codehausss
Contributor

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 this assertion for not contain and json path in the test step assertion - script assertion?

 

many thank

1 ACCEPTED SOLUTION

Accepted Solutions
rupert_anderson
Valued Contributor

Re: Groovy for assertion not contain and json path

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

 

 

Author of SoapUI Cookbook

View solution in original post

6 REPLIES 6
rupert_anderson
Valued Contributor

Re: Groovy for assertion not contain and json path

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

 

 

Author of SoapUI Cookbook

View solution in original post

codehausss
Contributor

Re: Groovy for assertion not contain and json path

hello Rupert,

 

many thanks for the sample script, it works.

 

for the ! operator, is this groovy operator or soapUI operator?

 


@rupert_anderson wrote:

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

 

 


 

 

rupert_anderson
Valued Contributor

Re: Groovy for assertion not contain and json path

Hi,

 

Many thanks for accepting the solution, glad it worked for you 🙂

 

Yes, thats the Groovy ! (not) operator - isn't that what you wanted to use?

 

Thanks,

Rup

Author of SoapUI Cookbook
codehausss
Contributor

Re: Groovy for assertion not contain and json path

yeah many thanks ruperts 🙂

master_ace85
Occasional Contributor

Re: Groovyscripts for Jsonpath works in Soapui but not in ReadyApi

Hi ;

 

 

The code below was used to resolvean issue that I had experienced before. Now the issue is the same groovy script is not working inReadyApi groovy script but  it does and still works in SOAPUI

 

The same code is used to replicate

 

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

 I get the above working fine in SOAPUI 5.3.0 but when I use the same script in ReadyAPI I get the following message: 

 

java.lang.NoClassDefFoundError: Could not initialize class net.minidev.json.JSONvalue error at ## (this would be the line that contains:

parse(json).read('$.quote.id')

 ) 

i read up another site that says it is missing the json-smart jar file but I confirmed that it is indeed in the lib installation dir of readyapi. I have even tried copying over SOAPUI's json-smart jar but it is an older version and it did not resolve my issue.

 

How can I resolve this? Surely this should be compatible or do I need to do something else?

 

nmrao
Community Hero
Moved:

Re: Groovy for assertion not contain and json path

cancel
Showing results for 
Search instead for 
Did you mean: