Building mock responses dynamically
As an alternative to conditionally selecting fixed structure mock responses, it is also very doable, but usually more effort, to construct responses dynamically. This can be useful when you want to vary the response structure in a way that would be tedious to hardcode. One example of this would be a mock implementation of a find/search resource; for example, /invoices?size=3
could return a response that contains a collection of size number of invoice documents. Then, the mockResponse
object, available in the MockResponse level script, could allow us to shape the response any way we want (see MockResponse at http://www.soapui.org/apidocs/index.html).
How to do it...
To achieve the earlier mentioned example, we can use Groovy's JSONBuilder
class to generate the collection of JSON invoice documents based on the size
parameter. Then, we'll set this on the mockResponse
object:
- We can add the new
/invoices/size=2
resource on theRESTContentNegotiation...