A brief overview of basic REST Assured usage and testing.
What is REST API?
- An API is an Application Programming Interface.
- REST is acronym for REpresentational State Transfer.
- REST determines what the API looks like.
What is REST Assured?
💡 A tool that aims to bring simplicity for testing and validating the response of REST services in Java.
- Runs on top of existing unit testing frameworks like JUnit or TestNG
- Can be used to test XML as well as JSON based web services.
- Provides support for all known HTTP methods.
- Supports Gherkin syntax
Test Setup
In your IDE, create a
new Maven project.
Dependencies in pom.xml
From
Maven Repository, copy the
latest stable versions of the following:
Paste them into your
pom.xml file and refresh.
Creating the Test
In the newly created project folder, create a
new class under
/src/test/java.
Static Imports
In order to use REST assured effectively it's recommended to statically import methods from the following classes:
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*
Gherkin Syntax Used In REST Assured
- Given() - 'Given' keyword, lets you set a background, here, you pass the request headers, query and path param, body, cookies. This is optional if these items are not needed in the request
- When() - 'when' keyword marks the premise of your scenario. For example, 'when' you get/post/put something, do something else.
- Method() - (get/post/put/delete)
- Then() - Your assert and matcher conditions go here
Common HTTP Methods
The verbs map to the
CRUD operations.
- GET retrieves resources.
- POST submits new data to the server.
- PUT updates existing data.
- DELETE removes data.
HTTP Status Codes and Error Messages
- (
100
–199
) Informational responses
- (
200
–299
) Successful responses
- (
300
–399
) Redirects
- (
400
–499
) Client errors
- (
500
–599
) Server errors
Very Simple GET Test
In this most basic test, I
- used GET to go to the website
- and asserted that statusCode is 200
If for whatever reason, it fails, then there is no need to look at any other test for this URL until this is fixed.
Simple POST Test
- Utilized JSONObjects to turn the inputted data into properly formatted JSON
- Output the newly formatted JSON to the console
- POST the data to the server
- Assert that the status code is 201
- and view the logs
Parameterization
- used in repeating the same test with different sets of data
- @DataProvider from TestNG is an easy way to feed test data to the actual tests
If you want to learn more about REST Assured, take a look at the
REST Assured usage guide on GitHub.
--
KaylaGilbert - 29 Jun 2020