Reputation: 2378
I would like to return a custom error message if an assertion fails in SoapUI
.
I have written assertions. I always get OK response even though the assertion fails.
I tried below script:
def assertionList = []
def idNotNull = (id!=null) ? "(id is not null" : assertionList.add("(id is null")
if(!assertionList.isEmpty())
{
return "exceptionResponse"
}
assert assertionList.isEmpty() : assertionList.toString()
But this will return before assert is executed. Hence assertion is passed though it should fail.
Is there a way that I can achieve this?
Upvotes: 3
Views: 2435
Reputation: 21389
It is because the script is returning only a message, but not making it failure. More over, return
should not be used here. Since there is return
, the assertion
statement in your code never reached.
Here is what you need to do:
There are two options you can choose as given below in the script
Here is the complete groovy script below, note that id
property is not found in the script you provided, so added to avoid property missing error.
def assertionList = []
def id
def idNotNull = (id!=null) ? "(id is not null" : assertionList.add("(id is null")
/**
* You may use one of the below two options
*/
//Option 1 : Using If condition fails, then Error
//not required to use isEmpty() like you did or null, by default it will check
if(assertionList){
throw new Error(assertionList.toString())
}
//Option 2:Using assertion
assert 0 == assertionList.size() : assertionList.toString()
Upvotes: 1