How to dynamically configure a REST mock in SoapUi

2019-07-16 14:18发布

问题:

Using SoapUi I intend to make a call to a MockService and when the Response is configured to return a static message, everything seems to be fine. On the other side, if I add and send a parameter (Eg. ?PName=John), although I also changed the name in the response from "John" to ${PName}, i get nothing (an empty string). Where am I doing it wrong? Please see the attached screenshot

Thank you,

回答1:

Here is how I would get the request parameters:

For example, user is passing a query parameter say test with value abc.

Mock Script:

//Read request parameter test and assign value to a variable
def userValue = mockRequest.httpRequest.getParameter('test')
//Assign the input value to response context
context.requestValue = userValue

Sample response using the context:

{
  "comment" : "${requestValue}"
}

Over all flow of data:



回答2:

Use concept of properties for parametrisation. Mock Services uses the properties Value. As your property PName is not present in Custom Properties at any level of soapUI, so you are getting NULL response.

Define a property PnameValue Project Level as

Now modify your Test Request value as

Add a Mock Service with response

Start your mock Service and Trigger operation.Also you can you use different Response based on Property Values at any level of SoapUI.

Please Accept answer if it solve your problem.