top of page
Search

How to use TestNG with RestAssured

  • Writer: Dev Raj Sinha
    Dev Raj Sinha
  • Jun 18, 2023
  • 2 min read

To use TestNG with RestAssured, you can leverage the TestNG framework's annotations and features to write and execute your RestAssured API tests. TestNG provides powerful test execution capabilities, such as parallel test execution, test grouping, data-driven testing, and more. Here's an example of how you can use TestNG with RestAssured:


1. Set up TestNG and RestAssured Dependencies:

First, make sure you have the necessary dependencies in your project. Include the TestNG and RestAssured dependencies in your project's `pom.xml` file:


<dependencies>
    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>

    <!-- RestAssured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Write Test Methods:

Create your test class and define test methods using TestNG annotations. Here's an example:


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

public class RestAssuredTest {

    @Test
    public void testGetUser() {
        Response response = RestAssured.get("https://api.example.com/users/1");
        int statusCode = response.getStatusCode();
        assertEquals(statusCode, 200);
    }

    @Test
    public void testCreateUser() {
        Response response = RestAssured.given()
            .contentType("application/json")
            .body("{\"name\":\"John Doe\",\"email\":\"johndoe@example.com\"}")
            .post("https://api.example.com/users");

        int statusCode = response.getStatusCode();
        assertEquals(statusCode, 201);
    }
}

In this example, we have two test methods: `testGetUser()` and `testCreateUser()`. Each test method is annotated with `@Test`, which indicates that it's a TestNG test method. Inside the test methods, we use RestAssured to send API requests and perform assertions on the response.


3. Run Tests with TestNG:

You can run your RestAssured tests using TestNG. You can either use an IDE with TestNG support (e.g., IntelliJ IDEA, Eclipse) or run the tests from the command line using Maven or Gradle.


If you're using an IDE, you can right-click on your test class or test suite file and select the option to run the tests with TestNG.


If you're using Maven, you can execute the following command to run the tests:


mvn clean test

If you're using Gradle, you can execute the following command:


gradle test

TestNG will execute your RestAssured tests, and you can view the test results in the console or HTML reports generated by TestNG.


By integrating TestNG with RestAssured, you can benefit from TestNG's powerful features for test execution and reporting while leveraging RestAssured for API testing.

 
 
 

Recent Posts

See All

Comments


Never Miss a Post. Subscribe Now!

Find new exciting ways to dominate the IT Automation industry

Thanks for submitting!

bottom of page