İçeriğe geç

Java ve Rest Assured ile API Testing

API (Application Programming Interface), iki uygulama arasında veri alışverişi yapılmasını sağlayan arayüzdür.
REST API ise HTTP protokolü kullanarak kaynak tabanlı veri transferi sağlar.

Rest Assured, Java tabanlı bir test kütüphanesidir ve RESTful API’lerin test edilmesini kolaylaştırır.
JUnit veya TestNG frameworkleri ile entegre çalışabilir.


GereksinimAçıklama
DilJava 8+
KütüphaneRest Assured
Test FrameworkTestNG veya JUnit
Build ToolMaven veya Gradle
Base URLhttps://reqres.in/api
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.0</version>
<scope>test</scope>
</dependency>

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
public class GetUsersTest {
@Test
public void getUsers() {
RestAssured.baseURI = "https://reqres.in/api";
Response response = RestAssured
.given()
.when()
.get("/users?page=2")
.then()
.statusCode(200)
.body("data", not(empty()))
.extract().response();
System.out.println("Response Body: " + response.asPrettyString());
}
}

Açıklama:

  • baseURI: API’nin temel adresidir.
  • given() → test ön koşullarını belirtir.
  • when() → HTTP isteğini gönderir.
  • then() → sonuç doğrulaması yapılır.

import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
public class PostUserTest {
@Test
public void createUser() {
String requestBody = "{ "name": "Mehmet", "job": "QA Engineer" }";
given()
.baseUri("https://reqres.in/api")
.contentType(ContentType.JSON)
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201)
.body("name", equalTo("Mehmet"))
.body("job", equalTo("QA Engineer"))
.log().all();
}
}

Notlar:

  • contentType(ContentType.JSON) → Gönderilen verinin türünü belirtir.
  • log().all() → isteği ve cevabı konsola yazdırır.

import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
public class PutUserTest {
@Test
public void updateUser() {
String requestBody = "{ "name": "Mehmet", "job": "Senior QA" }";
given()
.baseUri("https://reqres.in/api")
.contentType("application/json")
.body(requestBody)
.when()
.put("/users/2")
.then()
.statusCode(200)
.body("job", equalTo("Senior QA"))
.log().body();
}
}

import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
public class DeleteUserTest {
@Test
public void deleteUser() {
given()
.baseUri("https://reqres.in/api")
.when()
.delete("/users/2")
.then()
.statusCode(204)
.log().status();
}
}

import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class JsonPathExample {
@Test
public void validateResponseData() {
Response response = given()
.baseUri("https://reqres.in/api")
.when()
.get("/users/2")
.then()
.statusCode(200)
.extract().response();
JsonPath json = response.jsonPath();
String email = json.getString("data.email");
assertTrue(email.contains("@reqres.in"));
}
}

import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
public class ResponseValidationTest {
@Test
public void validateResponseTimeAndHeaders() {
Response response = given()
.baseUri("https://reqres.in/api")
.when()
.get("/users?page=2");
long responseTime = response.time();
System.out.println("Response Time: " + responseTime + " ms");
response.then()
.statusCode(200)
.header("Content-Type", "application/json; charset=utf-8");
}
}

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="API Test Suite">
<test name="Reqres API Tests">
<classes>
<class name="tests.GetUsersTest"/>
<class name="tests.PostUserTest"/>
<class name="tests.PutUserTest"/>
<class name="tests.DeleteUserTest"/>
</classes>
</test>
</suite>

Konsolda çalıştırma:

Terminal window
mvn test -DsuiteXmlFile=testng.xml

TestSonuçKodAçıklama
GET /usersPASS200Veri geldi
POST /usersPASS201Kullanıcı oluşturuldu
PUT /users/2PASS200Kullanıcı güncellendi
DELETE /users/2PASS204Kullanıcı silindi