Software Development and QA Tips By QASource Experts

How to Test Rest API with VS Code

Written by QASource Engineering Team | Jul 8, 2024 4:00:00 PM

Testing REST APIs is crucial to modern software development, ensuring the backend services work as expected. This guide provides a step-by-step process to set up API automation using Java with RestAssured and TestNG in Visual Studio Code (VS Code). By the end of this guide, you will be able to run automated API tests efficiently, ensuring the reliability and functionality of your APIs.

Step-by-Step Guide to Setting Up API Automation

Here’s a concise guide to setting up API automation using Java with RestAssured and TestNG in VS Code:

Step 1: Install Java, Maven, and VS Code

Before beginning, ensure you have Java, Maven, and Visual Studio Code installed on your machine. These tools are essential for developing and running your API tests.

Step 2: Open VS Code and Create a Maven Project

  1. Open VS Code.
  2. Navigate to the command palette (Ctrl+Shift+P) and select Maven: Generate from Archetype to create a new Maven project.
  3. Follow the prompts to set up your project structure.

Step 3: Add Dependencies for RestAssured and TestNG in pom.xml

To add the necessary dependencies for RestAssured and TestNG, edit the pom.xml file of your Maven project and include the following:

<dependencies>
 	<dependency>
 		<groupId>io.rest-assured</groupId>
		 <artifactId>rest-assured</artifactId>
		 <version>4.4.0</version>
 	</dependency>
 	<dependency>
		 <groupId>org.testng</groupId>
		 <artifactId>testng</artifactId>
		 <version>7.4.0</version>
		 <scope>test</scope>
	</dependency>
</dependencies>

Step 4: Write Java Code for API Methods

Create a new Java class ‘ApiService.java’ in ‘src/main/java/com/example/api’ and implement the API methods for GET, POST, and PUT requests.

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class ApiService {

 public Response getRequest(String url) {
 return RestAssured.get(url);
 }

 public Response postRequest(String url, String jsonPayload) {
 return RestAssured.given()
 .contentType(ContentType.JSON)
                .body(jsonPayload)
                .post(url);
    }

    public Response putRequest(String url, String jsonPayload) {
        return RestAssured.given()
                .contentType(ContentType.JSON)
                .body(jsonPayload)
                .put(url);
    }
}

Step 5: Write TestNG Test Class: Create a test class ‘ApiServiceTest.java’ in ‘src/test/java/com/example/api’ to write the test cases using TestNG

import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ApiServiceTest {

    ApiService apiService = new ApiService();

    @Test
    public void testGetRequest() {
        Response response = apiService.getRequest("paste your url");
        Assert.assertEquals(response.getStatusCode(), 200);
        System.out.println("GET Response: " + response.getBody().asString());
    }
}

Step 6: Run Your TestNG Tests

You can run your TestNG tests by right-clicking on the test file and selecting Run As > TestNG Test or using Maven:

Right-click TestNg and run testng
		OR
	mvn test

Conclusion

Following these steps, you can set up a robust API testing framework in VS Code using Java, RestAssured, and TestNG. This setup ensures that your API endpoints are tested for various scenarios, enhancing the reliability and performance of your backend services.