Q10 of 40 · REST Assured

What's the simplest way to log a failing request in REST Assured for debugging?

REST AssuredJuniorrest-assuredloggingdebuggingfundamentals

Short answer

Short answer: .log().ifValidationFails() on both given() and then() logs the full request and response only when an assertion fails — zero noise on passing tests. For always-on logging use .log().all(). Add it to RequestSpecBuilder so every test in the suite inherits it without per-test changes.

Detail

REST Assured's logging hooks onto System.out (or a custom PrintStream):

Always log (active debugging):

given().log().all()
.when().get(...)
.then().log().all()

Log only on failure (CI default — recommended):

given().log().ifValidationFails()
.when().get(...)
.then().log().ifValidationFails()

What .all() captures for requests: method, URI, headers (including auth), query params, body. For responses: status line, headers, body (pretty-printed JSON/XML).

Enable globally without touching every test — register a filter pair on RestAssured.filters():

RestAssured.filters(
    new RequestLoggingFilter(LogDetail.ALL),
    new ResponseLoggingFilter(LogDetail.ALL)
);

For CI pipelines, ifValidationFails() keeps logs clean and only surfaces detail when a test breaks.

// EXAMPLE

BaseApiTest.java

@BeforeAll
static void setup() {
    RestAssured.baseURI = System.getenv("API_BASE_URL");
    // Log request + response only when assertions fail — clean CI output
    RestAssured.filters(
        new RequestLoggingFilter(LogDetail.ALL),
        new ResponseLoggingFilter(LogDetail.ALL)
    );
}

// For one-off debugging — always-on logging for a specific test
@Test
void debugThisEndpoint() {
    given()
        .log().all()          // print request unconditionally
    .when()
        .get("/mystery-endpoint")
    .then()
        .log().all()          // print response unconditionally
        .statusCode(200);
}

// WHAT INTERVIEWERS LOOK FOR

Knowing ifValidationFails() vs always-on logging, and the filter-based approach for global suite-wide logging. Bonus: noting that auth headers appear in logs (useful for 401 debugging) and that sensitive headers can be masked with a custom filter.

// COMMON PITFALL

.log().all() left permanently on every test floods CI output with thousands of lines. Use ifValidationFails() globally and only enable .all() temporarily during active debugging.