REST API automation using the REST Assured library

What is REST Assured?

REST Assured is a Java Domain Specific Language (DSL) API used for writing automated tests that brings simplicity and ease to automating RESTful APIs.

We can send POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and validate the responses using REST Assured.

Let’s look at the sample code to understand how easily we can automate the HTTP request and response.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;
import org.testng.annotations.Test;
import io.restassured.response.Response;
import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
public class GETRequestTest {
private static Logger LOG = LoggerFactory.getLogger(GETRequestTest.class);
@Test
public void test_GET_sampleRequest() {
String uRL = "https://reqres.in/api/users";
RestAssured
.when()
.get(uRL)
.then()
.assertThat()
.statusCode(200)
.and()
.body("Michael", is(anything())).log().all();
}
}

Code explanation

Make a GET request for the target URL, assert that the status code is 200, and that the body contains a text that can be anything. Finally, log the response to the console.

RestAssured
            .when()
		    .get(uRL)
		    .then()
		    .assertThat()
		    .statusCode(200)
		    .and()
		    .body("Michael", is(anything())).log().all();

Free Resources