How to Test a REST Service
What is REST? Why is it Needed
REST (Representational State of Transfer) is a lightweight option for developing web services the uses the HTTP protocol –a fact that makes is simpler with less overhead than a web service that uses the SOAP protocol.
Today we'll be looking at soapUI but you can also programmatically test a rest service using a library like rest-assured without using a vendor tool.
(If you're interested in learning how to use soapUI to test SOAP web services, check out my post/video: soapUI – How to test a web service.)
For this example, I'll be using the Twitter API (GET statuses/user_timeline Resource) to perform a search for and return the last five tweets on my Twitter timeline:
How to use soapUI to test REST service
Here are the steps to follow to get started using the free version of soapUI with REST testing:
- Create a new project in soapUI named TWITTER REST (make sure you select the Opens dialog to create REST Service); click “OK”
- In the New REST Service dialog, enter the Service Endpoint:
- Click on the “Extract Resource/Method” and click “OK”
- On the New REST Resource dialog, click on the “Extract Params” button. You should have two parameters named id and count. Click “OK”
- In the New REST Method dialog, enter the Method Name, “GET JOE TIMELINE” and click “OK”
- Click on the “GET JOE TIMELINE” Request 1 and click on the green run arrow
- Cool! You should be able to see the XML results returned from the Twitter service:
Now let's add assertions to validate the response
In order to verify that the correct response has been returned, you'll need to add an assertion. For this example our assertion is going to verify that five twitter entries were indeed returned by response of the service.
- First, click on the request's “Add this REST request to a testcase” icon.
- Name the testsuite “TWITTER REST,” and the TestCase “GET TIMELINE.”
- Add the Request to TestCase. Accept all the default values and click “OK.”
- You should now have a TWITTER REST testsuite under your soapUI project:
Click on “GET JOE TIMELINE” Request and click “Assertions” then click on the “Add an assertion” icon.
- In the “Add Assertion” screen, click on Property
Content>XPath Match and click the “Add” button.
- For the XPath Expression, enter:
declare namespace ns1='http://www.w3.org/2005/Atom'; count(//ns1:feed/ns1:entry)
- Expected Result: enter “5.”
- Click the “Save” button and re-run.
- Your “assertion” should be green.
How to Test a REST service and Rest Easy
That's it! You can now rest easy in the knowledge that you now know how to test a REST service using soapUI!