Q10 of 40 · REST Assured
What's the simplest way to log a failing request in REST Assured for debugging?
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);
}