08 May 2007 - 1.7.1 home user-guide eclipse jbossws intellij netbeans maven 1.X/2.X PDF files forums bugs sourceforge






Vote for soapUI at the WSJ Readers' Choice awards in the

'Best Web Services Utility' and

'Best Web Services Testing Tool'

categories

Getting Started with Mocking

soapUI 1.7 introduces the possibility to create mock implementations of operations and entire services with a combination of static response messages combined with groovy scripts, attachments and custom http-headers. This opens for a number of usage scenarios, including:

  • Rapid Prototyping of Web Services; generate a complete static mock implementation from a WSDL in seconds and add dynamic functionality using Groovy.
  • Client testing/development; create mock implementations of desired operations and set up a number of alternative responses (including scripts, attachments and custom http-headers). Clients can be developed/tested without access to the "live" services. Responses can be cycled, randomized or selected with XPath expression from incoming request
  • Test-Driven Development; Create soapUI TestSuites/TestCases against MockServices before/during the actual services have been/are being implemented
  • etc..

In the following example we will create a mock implementation of an existing service, tailor the mock response with some groovy and call the mock service from within soapUI. Start by creating a new project for the publicaly available CurrencyConverter at http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

Create a MockService

After importing, you should get an interface with one operation and request. Right-click the interface and select the "Generate MockService" menu option:

This opens the following dialog which lets you specify the local port/path for the create service (can of course be changed later..);

Just leave the suggested values, for this and coming dialogs.. When the MockService has been created, you will get a new tree-hierarchy similar to the the initial Interface, but with grayed icons.. The MockService contains a MockOperation corresponding to the operation in the original interface and soapUI has also created one MockResponse for this operation from its schema.. Double click both the MockService and "Response 1" tree nodes to open their corresponding editors, after some moving/resizing arround you should be able to arrive at the following:

The MockService editor (top left) contains a toolbar, a list of all operations for the service and a request/response log which logs all incoming requests that have been handled by the MockService.

The MockResponse editor is very similar to the standard soapUI Request editor (which can/will be confusing at first ;-), only the "focus" has been shifted to the response-tabs of the editor, since this is where the editing of the mock response will be done. The "Incoming Request" tab/view shows the last received request to the operation (usefull for debugging client calls), including attachments nad http-headers.

Invoke the MockService from within soapUI

In the screen above you can see the default response that has been generated by soapUI from the schema. Lets continue by just invoking this mock operation from within soapUI:

  1. Click the green arrow button in the MockService editor.. this will start the service on the configured port/path which can also be seen in the log (this has already been done in the screenshot above..)
  2. In the MockResponse editor, select the left-most button in the toolbar, which will prompt to open one of the existing requests for the corresponding operation in your project. When opening the request, soapUI will automatically change its endpoint to the endpoint of the now locally running MockService
  3. Submit the opened request! If all goes well you should get the previously create MockResponse in the request editors response window:

Switching back to the MockResponse editor and selecting its "Incoming Request" tab/view, you should be able to see the request that was posted to the MockService and handled by the corresponding MockOperation:

Congratulations! Hopefully all went well and you now have your first MockService up and running.. Feel free to modify the created MockResponse content and resubmitting the request to see that your changes are instantly available (no need to restart the MockService)

Customize the MockResponse

Let's continue by making the created mock response a bit more dynamic with a groovy script. Switch to the "Response Script" tab in the MockResponse editor and enter the following:

Go back to the Mock Response tab and change the response body to

Now, when submitting a request to this MockResponse, the script will be evaluated and the generated rate value will be inserted into the outgoing response message;

Ok.. this should get you started with your first mock service.. now you can continue with:

  • Add some more responses to the MockOperation (via its right-button menu). Change the dispatch method through the MockOperation Options dialog
  • Add more dynamic script behaviour (database/file access, input processing, etc..), attachments, custom response http headers, etc..
  • Generate a web-service client with one of soapUI's tool integrations and call your MockService from there to see what message the client actually sends (and that it handles your response correctly)
  • etc..


Next: soapUI User Guide