How to Test Rest API with VS Code

How to Test Rest API with VS Code

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:


Step 4: Write Java Code for API Methods

Create a new Java class ‘’ 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()

    public Response putRequest(String url, String jsonPayload) {
        return RestAssured.given()

Step 5: Write TestNG Test Class: Create a test class ‘’ 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();

    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
	mvn test


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.


This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment