Rest Assured Java Methods – Complete Guide with
Examples
Introduction to Rest Assured
Rest Assured is a powerful Java library for testing RESTful APIs. It simplifies HTTP request
creation and response validation using a fluent, readable syntax.
Add Maven dependency:
[Link]-assured
rest-assured
5.4.0
test
Base Setup
import [Link];
public class BaseTest {
static {
[Link] = "[Link]
[Link] = 443;
}
}
HTTP Request Methods
GET – Retrieve Data:
given().when().get("/users/2").then().statusCode(200).body("[Link]", equalTo(2));
POST – Create Resource:
given().header("Content-Type", "application/json").body("{\"name\":\"Siva\",\"job\":\"QA\"}")
.when().post("/users").then().statusCode(201);
PUT – Update Resource:
given().header("Content-Type", "application/json").body("{\"job\":\"Senior QA\"}")
.when().put("/users/2").then().statusCode(200);
PATCH – Partial Update:
given().header("Content-Type", "application/json").body("{\"job\":\"Lead QA\"}")
.when().patch("/users/2").then().statusCode(200);
DELETE – Remove Resource:
when().delete("/users/2").then().statusCode(204);
Common Rest Assured Methods
given() – Define request specifications
when() – Define action
then() – Validate response
body() – Validate response body
header() – Add or verify headers
queryParam() – Add query parameter
pathParam() – Add path parameter
statusCode() – Check HTTP status
extract() – Extract data from response
log() – Log request or response
Authentication Methods
Basic Auth:
given().auth().basic("user","pass").when().get("/secure").then().statusCode(200);
Bearer Token:
given().header("Authorization","Bearer token").when().get("/profile").then().statusCode(200);
Assertions
.then().statusCode(200).body("[Link]", equalTo("test@[Link]")).body("[Link]",
notNullValue());
Response Extraction
Response response = given().when().get("/users/2");
[Link]("Status Code: " + [Link]());
[Link]("Body: " + [Link]().asString());
Best Practices
• Use Base URI for all tests.
• Keep JSON payloads in separate files.
• Use POJOs for request/response mapping.
• Add logging for debugging (.log().all()).
• Validate both status codes and response content.
• Integrate with TestNG/JUnit for automation suites.