Verbs & status codes
GET read POST create(201) PUT replace
PATCH partial DELETE remove(200/204)
200 OK 201 Created 400 bad request
401/403 auth 404 not found 5xx server broke
4xx -> suspect client. 5xx -> suspect server.
GET test
given()
.baseUri("https://jsonplaceholder.typicode.com")
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("Leanne Graham"))
.body("address.city", equalTo("Gwenborough"));
POST + extract (chaining)
int id = given()
.header("Content-Type", "application/json")
.body("""{ "title": "x", "userId": 7 }""")
.when().post("/posts")
.then().statusCode(201)
.extract().path("id"); // use in the next call
Matchers
equalTo(x) containsString("@") notNullValue()
greaterThan(0) hasSize(5)
body("$", hasSize(10)) // $ = whole response
body("[0].email", ...) // first array element
Debugging
.then().log().all() // prints request + response
// path() returns typed values: JSON numbers -> int
Params, headers, auth
given()
.queryParam("page", 2) // ?page=2
.pathParam("id", 7)
.header("Authorization", "Bearer " + token)
.auth().preemptive().basic("user", "pass")
.when().get("/posts/{id}")
PUT / PATCH / DELETE
.when().put("/posts/1") full replace expect 200
.when().patch("/posts/1") partial update expect 200
.when().delete("/posts/1") remove expect 200/204
.then().time(lessThan(2000L)) // response-time check
pom.xml rules
- rest-assured 6.0.0, scope test
- Declare rest-assured BEFORE junit-jupiter (Hamcrest)
- imports: io.restassured.RestAssured.* + org.hamcrest.Matchers.*