Sarvesh Sarvesh - 20 days ago 13x
Groovy Question

Assert a SoapUI test case with reason of failure

I have a test case on which I want to put the assertion.

I need to provide a reason for failure in assertion.

My output from XML format is as follow:

<soapenv:Envelope xmlns:soapenv="">
<faultstring>XYZ-001: input is wrong</faultstring>
<con:fault xmlns:con="">
<con:reason>input is wrong</con:reason>

My desired result should be the faultstring node of the xml.

For this I have tried with xpath assertion using this code:

declare namespace soapenv='';
declare namespace con='';

and I put true as expected output.
After generating JUnit report it was giving some other reason:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[XPath Match] XPathContains comparison failed for path [declare namespace soapenv='';
declare namespace con='';
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')], expecting [false], actual was [true]

I then proceed with Groovy using following script:

def groovyUtils = new context )
def requsetHolder = groovyUtils.getXmlHolder( messageExchange.requestContent )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def refNum = responseHolder.getNodeValue("soapenv:Envelope/soapenv:Body/soapenv:Fault/")
def testrunner = context.getTestRunner();
if (refNum != null){"soapenv:Envelope/soapenv:Body/soapenv:Fault/faultstring")

but no luck this time time also.
junit failure reason was:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[Script Assertion] net.sf.saxon.trans.XPathException: XPath syntax error at char 46 on line 2 in {}:
Unexpected token "<eof>" in path expression

so is there any way by which i can generate my custom reason in junit output using assertion in either groovy or xpath.

Rao Rao

Based on your question & comments, here is the Script Assertion.

  • The script contains different ways how to show the customized message in the report.
  • Please follow the in-line comments for details.
  • Sample code snippet added for additional check for specific errorCode element value if the response is a Fault. You can apply it for other elements too.

Script Assertion:

 * The below script should be used as Script Assertion
 * which checks if the response contains Fault, raise error otherwise
 * Once it has fault in it, then it checks for the specific "errorCode", raise error with
 * customized message

//Get the response parsed
def envelope = new XmlSlurper().parseText(context.response)

//There are three approaches to check & and throw customized error message
// if the response does not have Fault. Use one of them
assert envelope.Body.Fault, "Response does not have soap fault"
assert !envelope.Body.Fault.isEmpty(), "Response does not have soap fault"
if (!envelope.Body.Fault) { throw new Error ("Response does not have soap fault") }

//Further check for specific errorCode in the soap fault
def expectedErrorCode = 'XYZ-001'
def actualErrorCode = envelope.'**'.find { == 'errorCode' } as String "Actual code is : $actualErrorCode"
assert expectedErrorCode == actualErrorCode, "Soap fault should not have \"${expectedErrorCode}\""

You may quickly test it from here directly to see how it behaves if errorCode does not match.