Thursday, June 7

Adding validation code in SOAPUI mock services

If you have a requirement to run soap message validation within your SOAPUI mock service, the following steps will fulfill this requirement.

You might first start by viewing and implementing my blog here: Deploying a SOAPUI mock service as a web application to know how to get your mocks running and hosted outside of SOAPUI

1. Once you have created your SOAPUI mock service, double-click on the mock operation you wish to run the validation against. This should open up the MckOperation properties as shown in the second image below

 




2. Change the Dispath option (shown above) from Sequence to Script to open the groovy scripting window

3. Enter the following script which basically compares the request message to the mock WSDL, and raises an error if it does not validate. The script then informs the mock service on which response message to return if an error occurs

def wsdlcontext = context.mockService.getMockedInterfaces()[0].getDefinitionContext();
def validator = new com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator(wsdlcontext);
def msgExchange = new
com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockRequestMessageExchange(mockRequest,
mockOperation);
def errors = validator.assertRequest(msgExchange, false);
if (errors.length > 0 )
return "MockResponseInvalid2"
else
return "Response 10"


4. To create the error message called MockResponseInvalid2. Right-click in the mock responses window as shown below and click on 'New MockResponse' to create a new response

 


5. Give it a name that matches the name of the message you want  your mock to return when an invalid request comes through

 


6. At this point, you can either leave/modify the fault message or you can click on the SOAP fault button highlighted below to create a SOAP fault.

 


7. If you choose to create a SOAP fault, It asks if you want to overwrite the message. Click on Yes

8. Modify the resulting SOAP fault message as you wish.

 



9. You can now run valid and invalid requests against the mock operation to test your validation. Invalid request messages should return the soap fault message you just created