Answer
Automate GET Method in Rest Assured
Basic GET and Status Code Validation
Java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import static org.testng.Assert.*;
public class ApiTest {
@Test(description = "Verify status code for GET method")
public static void verifyStatusCodeGET() {
Response resp = given()
.when()
.get("https://reqres.in/api/users/2");
assertEquals(resp.getStatusCode(), 200);
}
}
Fluent Style (BDD Syntax)
Java
@Test
public void verifyGetStatusCode() {
given()
.when()
.get("https://reqres.in/api/users/2")
.then()
.statusCode(200);
}
With Base URI Setup
Java
@BeforeClass
public void setup() {
RestAssured.baseURI = "https://reqres.in";
}
@Test
public void getUser() {
given()
.when()
.get("/api/users/2")
.then()
.statusCode(200)
.log().all();
}
Key Methods
| Method | Purpose |
|---|---|
given() | Sets up request specification |
when() | Triggers the HTTP action |
get(url) | Sends GET request |
getStatusCode() | Returns the HTTP status code |
.then().statusCode(200) | BDD-style status assertion |
