← Back to libraryQuestion 232 of 468
🔌REST AssuredBeginner

Validating Status Code, Headers, and Response Time

📌 Definition:

REST Assured validates responses in then() with .statusCode(), .statusLine(), .header()/.headers(), .contentType(), and .time() — the first-line checks of any API test.

📖 Detailed Explanation:

then().statusCode(200) asserts the numeric status; .statusCode(is(oneOf(200, 201))) allows a set. .header('Content-Type', containsString('json')) and .contentType(ContentType.JSON) validate headers. .time(lessThan(2000L), TimeUnit.MILLISECONDS) asserts performance. .cookie('session', notNullValue()) checks cookies. These validations run against the actual response and fail the test with a clear message showing expected vs actual. Combining a status-code check with a body assertion is the minimum for a meaningful API test — a 200 with the wrong body still indicates a defect.

🔑 Key Points:
  • statusCode(), statusLine() validate status
  • header()/contentType() validate response headers
  • time(lessThan(ms)) asserts response time (SLA)
  • Combine status + body checks for meaningful validation
🌍 Real-World Example:

A smoke test asserts the health endpoint responds fast and correctly: when().get('/health').then().statusCode(200).time(lessThan(1000L), MILLISECONDS).body('status', equalTo('UP')).

🎯 Scenario-Based Interview Question:

Your test only does .statusCode(200) and passes, yet the API returned wrong data. What validation was missing and why does it matter?