0% found this document useful (0 votes)
42 views2 pages

Rest Assured Java Testing Guide

Rest Assured is a Java library designed for testing RESTful APIs, offering a fluent syntax for HTTP requests and response validation. The document outlines various HTTP methods (GET, POST, PUT, PATCH, DELETE), common methods for request specification, authentication techniques, and best practices for effective API testing. It emphasizes the importance of using a base URI, separating JSON payloads, and integrating with testing frameworks like TestNG or JUnit.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Rest Assured Java Testing Guide

Rest Assured is a Java library designed for testing RESTful APIs, offering a fluent syntax for HTTP requests and response validation. The document outlines various HTTP methods (GET, POST, PUT, PATCH, DELETE), common methods for request specification, authentication techniques, and best practices for effective API testing. It emphasizes the importance of using a base URI, separating JSON payloads, and integrating with testing frameworks like TestNG or JUnit.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like