Skip to main content

Day 13: Integrating Rest Assured with TestNG

Day 13: Integrating Rest Assured with TestNG

TestNG is a powerful testing framework that provides extensive features for test automation. Integrating Rest Assured with TestNG allows you to leverage the capabilities of both tools to create robust and efficient test suites for your API testing. In this tutorial, we will explore the process of integrating Rest Assured with TestNG and demonstrate how to write effective test cases.
To get started, follow these steps to integrate Rest Assured with TestNG:

Step 1: Setup Dependencies

First, ensure that you have the necessary dependencies in your project. You will need the following dependencies in your project's build configuration:

       
       

    io.rest-assured
    rest-assured
    {rest-assured-version}




    org.testng
    testng
    {testng-version}


Make sure to replace {rest-assured-version} and {testng-version} with the appropriate versions you are using in your project.

Step 2: Create TestNG Test Class

Create a new Java class for your Rest Assured tests and annotate it with @Test to mark it as a TestNG test class. You can also use other TestNG annotations like @BeforeClass, @AfterClass, etc., for setup and teardown operations.

       
       
       import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class RestAssuredTest {

    @BeforeClass
    public void setup() {
        // Set up Rest Assured configurations, if any
        RestAssured.baseURI = "https://api.example.com";
    }

    @Test
    public void testGetRequest() {
        // Write your Rest Assured test code here
        // Perform GET request and assertions
    }

    // Add more test methods as needed
}



                
Step 3: Write Rest Assured Test Cases

Inside the TestNG test class, you can now write your Rest Assured test cases. Use Rest Assured's fluent API to perform API requests, extract response data, and validate assertions.

       
     
     import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class RestAssuredTest {

    @BeforeClass
    public void setup() {
        // Set up Rest Assured configurations, if any
        RestAssured.baseURI = "https://api.example.com";
    }

    @Test
    public void testGetRequest() {
        given()
            .header("Content-Type", "application/json")
        .when()
            .get("/users/1")
        .then()
            .statusCode(200)
            .body("id", equalTo(1))
            .body("name", equalTo("John Doe"));
    }

    // Add more test methods as needed
}

                    

In the above example, we are performing a GET request to retrieve user details and asserting the response status code, as well as specific response body attributes.

Step 4: Execute TestNG Tests

You can now execute your Rest Assured tests using TestNG. You can run the tests directly from your IDE or use the TestNG command-line interface. Upon execution, TestNG will execute the Rest Assured test methods and generate detailed test reports, including pass/fail status, execution times, and more. By integrating Rest Assured with TestNG, you can take advantage

Comments

Popular posts from this blog

How to integrate Autoit with selenium

For handling Dialog boxes which are not web based, Then Autoit is the best Tool to handle this These are the following code should written in Selenium Selenium Code: try { String[] commands = new String[] {}; commands = new String[] { "Path" }; // location of Autoit EXE file Runtime.getRuntime().exec(commands); } catch (IOException e) { } Autoit code: if WinWaitActive("File Upload") Then ;MsgBox(2,"window found","Found the window") WinActivate("File upload") Send("!n") Sleep(5000) Send("File path") SEND("{ENTER}") ;location of the file you want to a to the form and submit ;Send("!O") Else MsgBox(1,"TimeOut","Timed out") EndIf Here we need to create a Exe file for the Autoit script and that path should be mentioned in selenium code.

Reflection API

The Reflection API allows Java code to examine classes and objects at run time.The new reflection classes allow you to call another class's methods dynamically at run time. With the reflection classes, you can also examine an instance's fields and change the fields' contents. The Reflection API consists of the java.lang.Class class and the java.lang.reflect classes: Field, Method, Constructor, Array, and Modifier. Example program: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; // calling method whose name we store in a variable public class ReflectionAPI { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String x="sampleTest"; //String meth=read name from xls file Method method= ReflectionAPI.class.getMethod(x, String.class); method.invoke(method, "welcome"); System.out.

Collection API

CollectionAPI: The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects. In a sense Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays. Example program: import java.util.ArrayList; import java.util.Hashtable; public class CollectionAPI { /** * @param args */ public static void main(String[] args) { int names[] = new int[5]; ArrayList list = new ArrayList (); list.add("Ramu");//0 list.add("Venu");//1 list.add("Raju");//2 System.out.println(list.size()); for(int i =0; i<=list.size(); i++) { System.out.println(list.get(i)); } // key - value // key - unique Hashtable table = new Hashtable (); table.put("name", "Hyderabad"); table.put("place", "Mumbai"); table.put("na