Monday 16 June 2014

Mocking your services

Almost all the developers which are using developing WebServices are well aware about SOAP UI, however I've always used only to send manual messages to some WebServices for manual testing.

SOAP UI much more than that and not even a licensed version is needed for this. A free one is enough.

Mocking your services is very important if you want to test your own services but you want to do it in isolation, basically without any of the external services (used by your own service to test) to cause of a less determined behavior. Another reason might be because you might want to execute performance test on your services and none of the external has to slow down the test itself.

Step by Step process:

  • Let's suppose you've got a service already available, if you don't have it just create a new simple one and deploy it somewhere.

The service in the following example accepts a String parameter which will be used for the mocking response, in your service you can add a String parameters or later you an use a similar approach with a different parameter type.


  • Create in Soap UI a new project and add the WSDL of the service. Here is my service which I am going to mock.





  • Right click at the port type in SOA UI of the imported WSDL and "Generate MockService". Port and Path can be customized. Select at least the operation you are going to mock.
  • As I did, create as many response as you need to verify your business rules. For instance

Some variable might be used in the response as showed below. SOAP UI uses Groovy as a script language then you might take a look of it.


  • After defining your answers, you have to define how these will be served to the caller; SoapUI provides several strategies:
    • RANDOM – serve a random answer
    • SEQUENCE – iterate through the available answers
    • QUERY_MATCH/XPATH – set up a mapping for each answer based on some queried request value
    • SCRIPT – create a Groovy script to determine the answer The most interesting (and easiest to set up) is the Groovy Script. I have to admit that it took me some time to come up with the script in a programming language I did not know before, but Groovy has a steep learning curve.


Above is my example, as you can see I've created a mapping

def symbolMap = [
"S":"Success" ,
"F":"Invalid parameters Fault" ,
"L":"Invalid login"
]

And in the script I get the first letter of the "Password" field and I use it as a key for the MAP above.
The result , which is the name of the Response, is returned back and SoapUI returns the correct Response to the client.

 
def xmlHolder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )

// extract the parameters from the request def

def string = xmlHolder["//*[local-name()='Password']"]

def reqSymbol = string.substring(0,1)



// request by symbol - lookup the appropriate response

log.info "The requested symbol (by symbol)=" + reqSymbol

// if mapping not found: null will be returned

def response = symbolMap[reqSymbol]

return response

  • Last step for you is to run and test your Mock Service. You've got two options:
    • Start the SOAP UI mock service. This can be started by right clicking at the mock port type and then Start mock service.
    • Deploy as a war from the Project context menu.