About this lesson
REST Assured PATCH request: a partial update that leaves other fields alone
Learn REST AssuredIntermediatePUT and PATCH
How it works
PATCH sends a set of changes instead of a full replacement. You name only the fields you want different, and the server merges them into the stored record. That makes it lighter than PUT and safer when several clients edit different fields of the same resource.
On testkru, PATCH /users/1 takes whatever JSON object you send and merges it over user 1, then returns the whole merged record with all five fields. Send only a new city and the response still carries John Doe, his email and age 30, with the city swapped. An empty object {} counts as no body at all and is refused with 400, as is a request with no JSON content type.
REST Assured has a patch() method next to get(), post() and put(), and the rest of the chain is identical: content type, body, path. Because the reply is the full record, one response lets you check both halves of the contract, the field you changed and the fields you did not touch.
About the assignment
Move user 1 to a new city with a one-field PATCH and prove from the response that the name survived the update unchanged.
What you will do
- Prepare the request with
given(), the JSON content type and a body holding just the newcityfrom the starter. - Send it with
patch()to/users/1. - Print the status code after
Status:. - Print the
namefield afterName:and thecityfield afterCity:, both read from the same response.
Common mistakes
Using put() by habit
The same one-field body sent with
put()is rejected with 400 because PUT requiresnameandemail. The verb is the whole point of this lesson.Patching with an empty object
Sending
{}to see what happens returns 400 with a message asking for fields to update. A PATCH needs at least one field.Only checking the changed field
Asserting that
citychanged proves half the behaviour. The bug PATCH tests exist to catch is a server that wipes the other fields, so assert on an untouched one such asnametoo.
In a real test suite
Partial updates show up in profile screens, status toggles and admin tools. In a JUnit suite a PATCH test usually asserts with body("city", equalTo(...)) and body("name", equalTo(...)) in one then() chain, so a single run checks both the change and the preservation.
Related lessons
- Full Update with PUTThe full-replacement PUT on the same user, for direct comparison.
- Validate Multiple FieldsChains several
body()matchers in onethen(), the pattern a PATCH check needs. - Handle PUT Validation ErrorShows what PUT does when a required field is missing.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: PATCH /users/1 with body: {"city": "San Francisco"}
// TODO: Print status, name (should still be "John Doe"), and city (should be updated)
}
}