Getting Started with REST Testing in SoapUI

What is a RESTful API?

REST stands for Representational State Transfer. It is a software architecture style that relies on a stateless communications protocol, most commonly, HTTP. REST structures data in XML, YAML, or any other format that is machine-readable, but usually JSON is most widely used. REST follows the object-oriented programming paradigm of noun-verb. REST is very data-driven, compared to SOAP, which is strongly function-driven. You may see people refer to them as RESTful APIs or RESTful web services. They mean the same thing and can be interchangeable. 

There is no standard for the description format of REST services. You can import your REST service in SoapUI by using WADL files. Besides, SoapUI Pro supports the OpenAPI, Swagger and RAML formats.

Your basic REST HTTP requests are: POST, GET, PUT, and DELETE. SoapUI supports HEAD, OPTIONS, TRACE and PATCH requests as well. Let’s look at an example from the Swagger Pet Store API:

  • Sending a GET request to /pet/{petId} would retrieve pets with a specified ID from the database.
  • Sending a POST request to /pet/{petId}/uploadImage would add a new image of the pet.
  • Sending a PUT request to /pet/{petId} would update the attributes of an existing pet, identified by a specified id.
  • Sending a DELETE request to /pet/{petId} would delete a specified pet.

So in a nutshell here is what each of these request types map to:

 

GET

Read or retrieve data

POST

Add new data

PUT

Update data that already exists

DELETE

Remove data

 
 

To learn more about REST requests and how to do them in SoapUI, please visit our Working with REST Requests page.

Getting Started with REST APIs

In general, REST testing is sending different requests to a REST API and verifying responses from it. SoapUI supports extensive testing of RESTful web services and their resources, representations, and so on.

This step-by-step tutorial will help you learn how to send requests to a REST API and verify its responses with assertions in SoapUI. Follow along below with a quick demo using the Google Maps API to get you going. In just a few minutes, you'll know how to create your first functional test of a RESTful web service — and it only takes a few clicks! Once you get the hang of it, start testing your own APIs!

To get started with this tutorial download SoapUI now!

If you’d like a more detailed guide to testing RESTful APIs, download our E-book: REST 101: The Beginner's Guide to Using and Testing RESTful APIs

REST Testing Video Tutorial

 

 

REST Testing Tutorial

Create REST Request

Start by creating a new REST project with a single request:

  1. –°lick Rest on the main toolbar or select File > New Rest Project.

    In the dialog box, enter the following URL and click OK:

    http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

    Dialog to create a new REST project

    SoapUI creates the project complete with a Service, Resource, Method and the actual Request and opens the Request editor.

  2. Click Run on the Request toolbar and you will see the XML output returned by the service:

    rest_places_resource_get_method_request_1

  3. The request seems fine, so let's create an actual functional test for this resource. Click Add to test case. Specify a name for the test suite, test case and for the REST Request test step:

    add_rest_request_to_test_case

  4. SoapUI creates a test case with a corresponding REST Request test step:

    run_rest_test_case

  5. Now double-click the resource icon in the navigator and change the Resource Path to "/maps/api/geocode/json":

    change_resource_path

  6. Open the created test step. Note that the path to resource has also changed. Send the request and check the response:

    json_rest_request

    Now you can see a nicely formatted JSON response in the JSON view to the right instead of the previous XML result.

Adding Assertion

Ok! Time to add an assertion to validate the content of the response. In our case we are just going to check that we get 1 place back from the service. In SoapUI Pro, you can create an assertion in a couple of clicks (see below to learn more). In SoapUI OS, perform the following steps:

  1. Open the REST Request test step and click Add assertion:

    Add assertion
  2. Select Property Content > JSONPath Count and click Add:

    Add assertion dialog
  3. In the dialog box, specify the following JSONPath expression:

    $['results'][*]
  4. Click Select from current to obtain the value from the current response:

    JSONPath count match configuration
  5. Click Save to create the assertion.

  6. Run the test case and check that the assertion passes:

    rest_test_case_completed_successfully

Adding Assertions in SoapUI Pro

This section explains how you can add assertions in SoapUI Pro. To try the product, download SoapUI Pro Trial.

  1. After you get a response, open the Outline tab. Right-click the node you want to verify and select Add Assertion > for Count: 

    Add count assertion to rest

  2. The appropriate assertion will be added and configured automatically. Check the generated JSONPath statement at the top and its expected result below and click Save:

    JSONPath Count Assertion

Creating Reports in SoapUI Pro

Finally, if you are running SoapUI Pro, you can create a simple HTML report:

  1. Open your test case and click Run.

  2. After test finishes, click Create Report button.

  3. In the subsequent dialog select JUnit-Style HTML Report and specify a folder where you want to save the report. Click OK.

    Creating JUnit-Style HTML Report

SoapUI will generate the report and open it in the default browser:

rest_test_case_junit_report

Voila! Your first functional test of a REST service with SoapUI, just a couple of clicks away.