Page 5 of 5
4) Customizing a MockResponse
Let's continue by making the created MockResponse a bit more dynamic by changing the dispatch and by Groovy scripting.
- First we'll create a second MockResponse.
- Give it a Groovy Name
- Let's write a script for the response. Click the Script tab in the Response and enter the following Groovy Script,
context.setProperty( "rate", Math.random() )
It will look like this:
This script sets a property called rate to a random number. As you might have noticed we do not use the property rate anywhere, so let's add it.
- In the ConversionRateResult element enter the following:
This is called a Property expansion and it's a very powerful feature in SoapUI. By inserting a property expansion in any element you can then fill the element programatically. Basically it tells SoapUI to use the current value of property rate.
- Now that we have written a script and the results will be inserted into the response, awesome!
That's it for now, but there is a lot of related material here on the soapui.org website, for example
- Creating Dynamic MockServices shows you how to add more "real-life" behaviour to your MockServices
- Mocking REST Services shows you how to mock REST or standard Web applications with SoapUI
- Property-Expansion gives you all details on how to use property-expansion in your SoapUI projects