soapui-service-mock-6

How to mock REST Service or SOAP Web Service using SOAPUI

Introduction

You can use SOAPUI for service mocking. In this tutorial, we will learn how to mock REST Service or SOAP Web Service using SOAPUI. Many a times in the real world scenario, both service provider and service consumer work simultaneously. However, if you notice, service consumer’s development work is pretty much dependent on the service provider. This is because the consumer needs to test the behavior with different response in different scenarios. Service mocking comes handy in such situations where service consumer can use a mock service and can test its various logic and behavior without depending on the service provider.

In this tutorial, we will learn how to do a simple mock of a REST service using SOAPUI. The same approach can be used for SOAP as well. So, let’s get started!!

Tech Stack

Only tool I will use here is SOAP UI. At the time when I am writing this tutorial, I am using version 5.4.0. You can always download the SoapUI tool from their official page. Choose your operating system and you are good to go.

Simple Service Mocking

In this section, we will create a simple service mocking example. We’ll mock a REST service. Check the request and response in below segment –

  • Step 1 Create an empty project –

    • Open SOAP UI tool and create an empty project by clicking on the Empty icon in the menu bar.
    • A new project with name “Project1” will be created
    • Right click on the project and choose “rename” item from the pop-up menu.
    • Provide a suitable name. In my case, I have provided the name as –> “RESTMockUserService”.
    • Click OK button.

      soapui-service-mock-1

      soapui-service-mock-1

  • Step 2 Create Mock Service

    • Right click on the newly created project above and select “New REST MockService”
    • Rename the mock service name to “UserServiceMock”. Click OK button.
    • This will create a new mock service.

      soapui-service-mock-2

      soapui-service-mock-2

  • Step 3 Create new mock Action

    • Right click on the “UserMockService” created above and select “Add new mock action”
    • A new popup will open up.
      • Select the “Method” as GET
      • In resource path, type in: /user/1234
      • Clock OK button.
    • A new mock action with GET method will be created. If you expand the “UserServiceMock”, you will notice something like below –

      soapui-service-mock-3

      soapui-service-mock-3

  • Step 4 Create new mock response

    • Right click on “/user/1234” GET action and select “New MockResponse”.
    • A new popup will open. Provide the Mock Response Name as “User1234”. Click OK button.
    • A new window will open up. In the new window –
      • Choose the HTTP status code as 200 – OK
      • Content | Media type as “application/json”. This is because, I will demo using a JSON response. If you would like to use any other format, choose the content type accordingly.
      • In the bottom Editor, provide the response that you want to send back to consumer. In my case, I have provided

         
      • Now the screen looks like –

        soapui-service-mock-4

        soapui-service-mock-4

  • Step 5 Start the mock service

    • Now double click on the “UserServiceMock” mock service. A new window will open up. Click on the green arrow button.

      soapui-service-mock-5

      soapui-service-mock-5

    • This will start the mock service and when it starts successfully, you will see a window like below. Notice the port number on which the service started listening to. In my case, it is 8080.

      soapui-service-mock-6

      soapui-service-mock-6

  • Step 6 Test the mock

    • Well, now that the service started, it’s time to test.
    • Open up a browser and in the address bar, type: http://localhost:8080/user/1234
    • You will notice response JSON body in the browser. This is the same response that we mocked.

      soapui-service-mock-7

      soapui-service-mock-7

 

130

No Responses

Write a response

This site uses Akismet to reduce spam. Learn how your comment data is processed.