Skip to main content

Use TestNG Assertions with REST Assured

While automating APIs using REST Assured in several situations you find you need to assert the things outside of normal flow where you don't mention equalTo() inside .body().

In that case you can use TestNG or JUnit Assertions. There is nothing special things you need to do here you just need to receive the response in some variable or using some method and assert it.

In below example you can see I am using TestNG assert to validate one of the header and body key:

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class ReqRes 
    public void setup()
        RestAssured.baseURI = "";
        RestAssured.basePath = "api";
    public void TestPathParametersExample()
        Response res = given()
	        .queryParam("page", "2")
        JsonPath js = new JsonPath(res.asString());
        Assert.assertEquals(js.getString("total_pages"), "2");
        Assert.assertEquals(res.getHeader("server"), "cloudflare");		
Video Tutorial


Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.