Mock Service SOAP UI
Introduction
Mock Service implements what is also called: Mock, Stub, Fake, service simulation, stub
Example
Let us make a service that will send us messages in response to a certain type of request
to a certain EndPoint
For the example I chose a service for a bicycle site
TopBicycle.ru
visit that site if you love bicycles.
Similar functionality can be implemented with any web server.
If you do not want to deal with SOAP UI, I recommend reading the article
Flask
File → Create Empty Project
Enter the project name
New Rest Mock Service
Specify the MockService name
Add new mock action
Enter path and choose the method GET
New MockResponse
Enter the name of the new response
Copy json and paste it into the response body
{
"bicycles":{
"bicycle": [{
"id":1,
"name": " Helkama ",
"country": " Finland"
}, {
"id":2,
"name": " Stels ",
"country": "Russia"
},{
"id":3,
"name": " Tunturi ",
"country": " Finland "
}]
}
}
Replace Content | Media type with application/json. The Http Status Code must be 200 – OK. Enter Headers as you wish. They can be added by clicking on the green plus +
Right-click on BicycleService and
Add new mock action
Name it «bicycle/1»
Right-click on «bicycle/1» (or press Ctrl + N) and
create New MockResponse
name it «singleBicycle»
Copy json and paste it into
the response body
{
"bicycle": {
"id":1,
"name":"Helkama",
"country":"Finland"
}
}
Create a new mock action, leave the old name «/bicycle» and change the type to
POST
instead of GET.
Create a new MockResponse and name it «addBicycle»
Copy json and paste it into the response body
{"key":12041961}
Create a new mock action of type GET and name it «/incorrectep» and a new MockResponse «400» Change Http Status Code to 400 – Bad Request
Note that the BicycleService service uses the port 8080
Start BicycleService by clicking the green triangle
We have four scenarios
Use Postman to test them
POST to http://localhost:8080/bicycle
It should return the key 12041961
GET to http://localhost:8080/bicycle
It should return the list of bicycles
GET to http://localhost:8080/bicycle/1
It should return the first bicycle
GET to http://localhost:8080/incorrectep
It should return an empty response and Http Status 400 Bad Request
Dynamic Response
To replace the static response 12041961 with a dynamic one in the POST request to /bicycle let us replace 12041996 with ${variable} and add the following code to script:
context.setProperty("variable" ,Math.random()*100000000000000000)
To test it let us send several requests from Postman and check whether the response changes
Article author: Andrei Olegovich