About this lesson
REST Assured: log the response on failure with then().log().ifValidationFails()
Learn REST AssuredAdvancedLogging
How it works
Request logging tells you what you sent; response logging tells you what the server said back, and for a failing API test that is usually the more useful half. After then(), log() returns a response log specification, and ifValidationFails() makes it print only when a later assertion in the same chain fails. Placed before the checks, it covers all of them.
The failure output is generous. When we expected the wrong category name for product 1, REST Assured printed the status line, every response header (rate-limit counters, Content-Type, the server name and more) and then the JSON body, pretty-printed with indentation, before the AssertionError. With a wrong user ID it showed the 404 status and the server's error message in the body, which explains the failure faster than any assertion text.
One detail of the error itself: when the status check failed on that 404, the assertion message reported one expectation, the status, even though the name check would also have failed. Fix the status problem first and the next run tells you about the rest.
About the assignment
Validate two different endpoints with response logging that only prints on failure, and print a confirmation line after each one passes.
What you will do
- Send a GET for
/users/1, then afterthen()calllog().ifValidationFails()before the assertions. - In that chain, check status 200 with
statusCode()and thenamefield withbody()andequalTo(), then print the first confirmation line from the expected output. - Repeat for
/products/1: same logging call, status 200, and abody()check on the nestedcategory.namepath against the expected category. - Print the second confirmation line after the product chain.
Common mistakes
Checking the category object itself
In the JSON product,
categoryis an object with anidand aname. Comparing the whole object to a string fails; use the dotted path down to thenamefield.Printing without asserting
If the chain stops at
then()with no matchers, nothing is checked and the confirmation line proves nothing. The line should only be reachable after real assertions.Looking for logs on a green run
Both requests pass here, so the console shows only your two lines. That silence is the feature; change an expected value temporarily to watch the log appear.
In a real test suite
In a JUnit 5 suite this logging usually lives in a shared response specification or a global setting rather than in each test. When a test fails in CI at night, the log already holds the headers and body from that exact moment, which matters for problems that do not reproduce later, such as rate limiting or a deployment in progress.
Related lessons
- Conditional Request LoggingUses failure-only logging on the request side and extracts a value from the chain.
- Validate Nested JSONValidates nested JSON fields with dot paths such as the category name.
- Basic Response SpecificationMoves repeated status and body checks into a reusable response specification.
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 static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /users/1 with .then().log().ifValidationFails()
// Validate statusCode(200) and body("name", equalTo("John Doe"))
// Print "Response validation passed"
// TODO: GET /products/1 with .then().log().ifValidationFails()
// Validate statusCode(200) and body("category.name", equalTo("electronics"))
// Print "Product validation passed"
}
}