Your test failed. The status code is 400 instead of 200. But what did you actually send? What did the server actually return? Without logging, you are debugging blind. REST Assured has built-in logging that shows you everything.
@Test
public void testWithRequestLogging() {
given()
.log().all() // Logs method, URI, headers, body — EVERYTHING
.contentType(ContentType.JSON)
.body(Map.of("title", "Debug Me", "userId", 1))
.when()
.post("/posts")
.then()
.statusCode(201);
}
// Console output:
// Request method: POST
// Request URI: https://jsonplaceholder.typicode.com/posts
// Headers: Content-Type=application/json; charset=UTF-8
// Body:
// {
// "title": "Debug Me",
// "userId": 1
// }@Test
public void testWithResponseLogging() {
given()
.contentType(ContentType.JSON)
.when()
.get("/posts/1")
.then()
.log().all() // Logs status, headers, body
.statusCode(200);
}
// Console output:
// HTTP/1.1 200 OK
// Content-Type: application/json; charset=utf-8
// Body:
// {
// "userId": 1,
// "id": 1,
// "title": "sunt aut facere...",
// "body": "quia et suscipit..."
// }| Method | What It Logs | When to Use |
|---|---|---|
| .log().all() | Everything — method, URI, headers, body | Full debugging session |
| .log().uri() | Only the request URL | Quick check — am I hitting the right endpoint? |
| .log().headers() | Only the headers | Debugging auth or content-type issues |
| .log().body() | Only the request/response body | Checking what JSON was sent/received |
| .log().status() | Only the status line | Quick pass/fail check |
| .log().ifError() | Logs only when status >= 400 | Production-safe — silent when tests pass |
| .log().ifValidationFails() | Logs only when an assertion fails | Best for CI — no noise on success |
@Test
public void testSmartLogging() {
given()
.log().ifValidationFails() // Only log request if assertions fail
.contentType(ContentType.JSON)
.when()
.get("/posts/1")
.then()
.log().ifValidationFails() // Only log response if assertions fail
.statusCode(200)
.body("id", equalTo(1));
}
// If tests PASS: console is clean, no noise
// If tests FAIL: you see the full request and response — exactly what you need// Enable logging for ALL requests in the entire test suite
@BeforeClass
public void setupGlobalLogging() {
// Log all requests and responses (good for debug, bad for CI)
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
// Or set it globally
RestAssured.config = RestAssured.config()
.logConfig(LogConfig.logConfig()
.enableLoggingOfRequestAndResponseIfValidationFails());
}Key Point: Use .log().ifValidationFails() on both request and response. Silent when tests pass. Full details when they fail. This is the professional approach — clean CI logs with no noise, but all the info you need when something breaks.
Never use .log().all() in CI/CD pipelines permanently. It prints every request and response body, including sensitive data like auth tokens and passwords. Use .log().ifValidationFails() or .log().ifError() instead.
Q: How do you debug a failing REST Assured test?
A: First, I add .log().all() after given() and after then() to see the full request and response. This shows me the exact URL, headers, body I sent, and what came back. For permanent use, I switch to .log().ifValidationFails() — it only logs when assertions fail, keeping CI logs clean. I also check: is the base URI correct? Are headers set? Is the body formatted as JSON? For auth issues, .log().headers() shows if the token is being sent. REST Assured also supports global logging via RestAssured.enableLoggingOfRequestAndResponseIfValidationFails().
Key Point: Use .log().all() for active debugging. Use .log().ifValidationFails() for production tests. Never log everything in CI — it leaks sensitive data and creates noise.