Swagger UI Api Testing with Rest Assured

Emre Dogangun
5 min readFeb 27, 2024

--

RestAssured is a free library developed in Java specifically designed for testing and validating HTTP requests made using RESTful APIs. It is widely preferred by software developers and test engineers because it makes communicating with and testing RESTful services very easy. RestAssured provides a simple, clean and flexible programming interface that allows users to create test cases quickly and efficiently. Additionally, BDD (Behavior Driven Development) enthusiasts will love this library as it has a Gherkin-type syntactic structure.

The main features of RestAssured are:

  1. Ease of use: RestAssured’s syntax is very easy to use. Interacting with RESTful services is now easier. You can quickly create test scenarios without having to deal with complex HTTP requests and responses.
  2. Request and Response Handling: RestAssured can easily handle a variety of HTTP methods (GET, POST, PUT, DELETE, etc.) and various data types (form parameters, header information, body data, etc.). Itcanal so examine requests and responses in detail.
  3. Support for path and query parameters : Easily handle path and query parameters that are frequently used when working with RESTful services. In this way, you can use RestAssured and path and query parameters tocreate and test dynamic API requests suitable for different scenarios. This flexibility allows for more thorough API testing, especially in a variety of situations.
  4. JSON and XML processing: RestAssured easily handles interactions with data formats such as JSON and XML commonly used in RESTful APIs. This feature is crucial for understanding and validating API responses and creating test scenarios.
  5. Automation and integration: RestAssured is designed to be used in test automation projects. Therefore, it can be easily integrated with common test automation tools (TestNG, JUnit, etc.) and CI/CD processes to automate test scenarios.

Creating pom.xml

Create a Maven project in Java and add a dependency to the pom.xml file as follows. Here the JUnit framework is used, but you can choose another framework if necessary.

<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>

How to Send a GET Request with RestAssured?

Now create a new package in the src/test/java/pet folder. Add your classes to the package you created and create your first test method together.

https://petstore.swagger.io/#/pet/getPetById Let’s write our test methods for the get endpoints of the swagger services at this address.

 @Test
public void getPetTest() {
RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

Response response = httpRequest.request(Method.GET, "v2/pet/1");

int statusCode = response.getStatusCode();
System.out.println("Response status code is " + statusCode);

Headers header = response.getHeaders();
System.out.println("Headers of the response body are " + header);

ResponseBody body = response.getBody();
body.prettyPrint();
System.out.println("Response Body is " + body.asString());

}

RestAssured.baseURI: This line specifies the base URI of the API for testing.

Response response = httpRequest.request(Method.GET, “v2/pet/1”): This line sends an HTTP GET request to the specified URL using the RestAssured library and returns a Response object representing the response to the request.

response.getStatusCode(): This retrieves the HTTP response status code. For example, a status code such as 200 OK or 404 Not Found.

https://petstore.swagger.io/#/pet/findPetsByStatus Let’s write our test methods for the get endpoints of the swagger services at this address.

 @Test
public void getFindByStatusTest1() {
RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

Response response = httpRequest.request(Method.GET, "v2/pet/findByStatus?status=sold");

int statusCode = response.getStatusCode();
System.out.println("Response status code is " + statusCode);

Headers header = response.getHeaders();
System.out.println("Headers of the response body are " + header);

ResponseBody body = response.getBody();
body.prettyPrint();
System.out.println("Response Body is " + body.asString());

}
@Test
public void getFindByStatusTest2() {
RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

Response response = httpRequest.request(Method.GET, "v2/pet/findByStatus?status=pending");

int statusCode = response.getStatusCode();
System.out.println("Response status code is " + statusCode);

Headers header = response.getHeaders();
System.out.println("Headers of the response body are " + header);

ResponseBody body = response.getBody();
body.prettyPrint();
System.out.println("Response Body is " + body.asString());

}
 @Test
public void getFindByStatusTest3() {
RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

Response response = httpRequest.request(Method.GET, "v2/pet/findByStatus?status=available");

int statusCode = response.getStatusCode();
System.out.println("Response status code is " + statusCode);

Headers header = response.getHeaders();
System.out.println("Headers of the response body are " + header);

ResponseBody body = response.getBody();
body.prettyPrint();
System.out.println("Response Body is " + body.asString());

}

How to Send a POST Request with RestAssured?

POST: Used to create a new record.

https://petstore.swagger.io/#/pet/addPet Let’s write our test methods for the post endpoints of the swagger services at this address.

@Test
public void postPetTest() {
RestAssured.baseURI = "https://petstore.swagger.io";

String request = "{\n" +
" \"id\": 0,\n" +
" \"category\": {\n" +
" \"id\": 0,\n" +
" \"name\": \"string\"\n" +
" },\n" +
" \"name\": \"doggie\",\n" +
" \"photoUrls\": [\n" +
" \"string\"\n" +
" ],\n" +
" \"tags\": [\n" +
" {\n" +
" \"id\": 0,\n" +
" \"name\": \"string\"\n" +
" }\n" +
" ],\n" +
" \"status\": \"available\"\n" +
"}";

RequestSpecification httpRequest = RestAssured.given();

httpRequest.header("Content-Type","application/json");

Response response = httpRequest.body(request).post("/v2/pet");

int statusCode = response.getStatusCode();
System.out.println("Response status code is "+statusCode);

}
@Test
public void postPetTest1() {
RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

httpRequest.header("Content-Type","application/json");

Response response = httpRequest.post("/v2/pet/1");

int statusCode = response.getStatusCode();
System.out.println("Response status code is "+statusCode);

}

https://petstore.swagger.io/#/pet/deletePet Let’s write our test methods for the delete endpoints of the swagger services at this address.

  @Test
public void deletePetTest() {

RestAssured.baseURI = "https://petstore.swagger.io";

RequestSpecification httpRequest = RestAssured.given();

httpRequest.header("Content-Type", "application/json").header("accept", "application/json");

Response response = httpRequest.delete("/v2/pet/1");

System.out.println(response);
System.out.println("Response status code is " + response.getStatusCode());

}

https://petstore.swagger.io/#/pet/updatePet Let’s write our test methods for the put endpoints of the swagger services at this address.

 @Test
public void putPetTest() {
RestAssured.baseURI = "https://petstore.swagger.io";

String request = "\n" +
" \"id\": 1,\n" +
" \"category\": {\n" +
" \"id\": 0,\n" +
" \"name\": \"string\"\n" +
" },\n" +
" \"name\": \"doggie\",\n" +
" \"photoUrls\": [\n" +
" \"string\"\n" +
" ],\n" +
" \"tags\": [\n" +
" {\n" +
" \"id\": 0,\n" +
" \"name\": \"string\"\n" +
" }\n" +
" ],\n" +
" \"status\": \"available\"\n" +
"}";

RequestSpecification httpRequest = RestAssured.given();

httpRequest.header("Content-Type", "application/json").header("accept", "application/json");

Response response = httpRequest.body(request).put("/v2/pet");

System.out.println(response);
System.out.println("Response status code is " + response.getStatusCode());

}

Github Link: https://github.com/dogangunemre/restAssured-SwaggerUI/

--

--