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

SOAP UI Create Empty Project image from www.eth1.ru

Enter the project name

SOAP UI Enter Project Name image from www.eth1.ru

New Rest Mock Service

SOAP UI New Rest Mock Service image from www.eth1.ru

Specify the MockService name

SOAP UI Specify name of MockService image from www.eth1.ru

Add new mock action

SOAP UI Add new mock action image from www.eth1.ru

Enter path and choose the method GET

SOAP UI Enter Resource path image from www.eth1.ru

New MockResponse

SOAP UI New MockResponse image from www.eth1.ru

Enter the name of the new response

SOAP UI Enter name of new MockResponse image from www.eth1.ru

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 +

SOAP UI Copy the json below and paste it to the Response body image from www.eth1.ru

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" } }

SOAP UI new mock response image from www.eth1.ru

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}

SOAP UI image from www.eth1.ru

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

SOAP UI Http Status Code 400 - Bad Request image from www.eth1.ru

Note that the BicycleService service uses the port 8080

SOAP UI Http Status Code 400 - Bad Request image from www.eth1.ru

Start BicycleService by clicking the green triangle

SOAP UI start REST Mock service image from www.eth1.ru

We have four scenarios

Use Postman to test them

POST to http://localhost:8080/bicycle

It should return the key 12041961

SOAP UI image from www.eth1.ru

GET to http://localhost:8080/bicycle

It should return the list of bicycles

SOAP UI image from www.eth1.ru

GET to http://localhost:8080/bicycle/1

It should return the first bicycle

SOAP UI image from www.eth1.ru

GET to http://localhost:8080/incorrectep

It should return an empty response and Http Status 400 Bad Request

SOAP UI image from www.eth1.ru

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)

SOAP UI script image from www.eth1.ru

To test it let us send several requests from Postman and check whether the response changes

Postman request image from www.eth1.ru

Article author: Andrei Olegovich