๐ API Testing
REST API fundamentals, Postman, RestAssured (given/when/then), JSON validation, and status codes.
What is API Testing?
API testing evaluates Application Programming Interfaces (APIs) directly for functionality, reliability, performance, and security without relying on a graphical user interface (GUI).
HTTP Methods & Status Codes
Understanding standard REST API requests and HTTP response codes.
| Method | Purpose | Common Success Code | Common Error Code |
|---|---|---|---|
| GET | Retrieve data from server | 200 OK | 404 Not Found |
| POST | Create new resource | 201 Created | 400 Bad Request |
| PUT | Update / Replace resource | 200 OK | 401 Unauthorized |
| DELETE | Remove resource from server | 204 No Content | 403 Forbidden |
Automating REST APIs with RestAssured
RestAssured provides a BDD syntax (`given()`, `when()`, `then()`) for testing REST services in Java.
ApiTest.java
Java
import io.restassured.RestAssured; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; import org.testng.annotations.Test; public class ApiTest { @Test public void testGetUser() { RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; given() .header("Content-Type", "application/json") .when() .get("/users/1") .then() .statusCode(200) .body("name", equalTo("Leanne Graham")) .body("address.city", notNullValue()); } }