You can GET and POST. Now let us complete the CRUD cycle. PUT replaces an entire resource. PATCH updates part of it. DELETE removes it. With these four, you can test any REST API completely.
PUT replaces the entire resource. You must send ALL fields, even the ones that did not change. Think of it like rewriting an entire document instead of editing a paragraph.
@Test
public void testUpdatePostWithPut() {
Map<String, Object> updatedPost = new HashMap<>();
updatedPost.put("id", 1);
updatedPost.put("title", "Updated Title");
updatedPost.put("body", "Updated body content");
updatedPost.put("userId", 1); // Must send ALL fields for PUT
given()
.contentType(ContentType.JSON)
.body(updatedPost)
.pathParam("id", 1)
.when()
.put("/posts/{id}")
.then()
.statusCode(200)
.body("title", equalTo("Updated Title"))
.body("body", equalTo("Updated body content"));
}PATCH sends only the fields you want to change. Everything else stays the same. Like editing one paragraph in a document without rewriting the rest.
@Test
public void testPartialUpdateWithPatch() {
Map<String, Object> partialUpdate = new HashMap<>();
partialUpdate.put("title", "Only Title Changed");
// NOT sending body or userId — they stay as-is
given()
.contentType(ContentType.JSON)
.body(partialUpdate)
.pathParam("id", 1)
.when()
.patch("/posts/{id}")
.then()
.statusCode(200)
.body("title", equalTo("Only Title Changed"))
.body("userId", equalTo(1)); // userId unchanged
}@Test
public void testDeletePost() {
given()
.pathParam("id", 1)
.when()
.delete("/posts/{id}")
.then()
.statusCode(200); // JSONPlaceholder returns 200 for delete
// Some APIs return 204 No Content for successful deletes
}
@Test
public void testDeleteAndVerify() {
// Step 1: Delete the resource
given()
.pathParam("id", 1)
.when()
.delete("/posts/{id}")
.then()
.statusCode(200);
// Step 2: Try to GET it — should be 404
// Note: JSONPlaceholder is a fake API, so this still returns 200
// On a real API, you would expect 404 here
given()
.pathParam("id", 1)
.when()
.get("/posts/{id}")
.then()
.statusCode(404); // Resource no longer exists
}@Test
public void testCompleteCRUDCycle() {
// CREATE
int postId =
given()
.contentType(ContentType.JSON)
.body(Map.of("title", "CRUD Test", "body", "Testing all operations", "userId", 1))
.when()
.post("/posts")
.then()
.statusCode(201)
.extract().jsonPath().getInt("id");
System.out.println("Created post ID: " + postId);
// READ
given()
.pathParam("id", postId)
.when()
.get("/posts/{id}")
.then()
.statusCode(200)
.body("title", equalTo("CRUD Test"));
// UPDATE
given()
.contentType(ContentType.JSON)
.body(Map.of("title", "Updated CRUD Test"))
.pathParam("id", postId)
.when()
.patch("/posts/{id}")
.then()
.statusCode(200)
.body("title", equalTo("Updated CRUD Test"));
// DELETE
given()
.pathParam("id", postId)
.when()
.delete("/posts/{id}")
.then()
.statusCode(200);
}JSONPlaceholder is a fake API — it does not actually persist data. Your POST creates nothing real. Your DELETE deletes nothing. But the status codes are correct, so it is perfect for learning. When you test a real API, the CRUD cycle will actually create and delete resources.
In a real project, chain CRUD operations in a @Test method: create a resource, read it, update it, verify the update, delete it, verify deletion. This is the "happy path CRUD test" and every API test suite should have it.
Q: What is the difference between PUT and PATCH?
A: PUT replaces the entire resource. You must send ALL fields in the body, even unchanged ones. If you miss a field, it may be set to null. PATCH sends only the fields you want to update — everything else remains unchanged. Example: if a user has name, email, and phone, PUT requires all three fields. PATCH can send just {email: "new@email.com"} and name/phone stay as-is. Both are used for updates, but PATCH is more efficient for partial changes. PUT is idempotent by definition. PATCH may or may not be.
Key Point: PUT replaces everything (send all fields). PATCH updates partially (send only changed fields). DELETE removes. Together with GET and POST, you have the complete CRUD testing toolkit.