About this lesson
REST Assured ResponseSpecBuilder: shared expectations for every response
Learn REST AssuredIntermediateResponse Specification
How it works
Request specs remove repetition from what you send. Response specs do the same for what you check. ResponseSpecBuilder collects expectations such as expectStatusCode(), expectContentType(), expectHeader() and expectBody(), and build() returns a ResponseSpecification. You apply it on the validation side with then().spec(), and REST Assured runs every expectation in it against the response.
The content-type expectation is forgiving in a useful way. ContentType.JSON matches a header of application/json followed by a charset=utf-8 parameter, so the charset suffix the server adds does not break it. Point the same spec at an XML endpoint, though, and it fails, and the message quotes the actual content type.
When several expectations fail at once, REST Assured does not stop at the first one. The AssertionError starts with a count, such as two expectations failed, and lists each broken rule, for example both a wrong status and a wrong content type. That makes a failed spec check quick to diagnose.
About the assignment
Define one response specification for a successful JSON reply, validate three collection endpoints against it, and confirm each validation.
What you will do
- Create a
ResponseSpecBuilder, addexpectStatusCode()with 200 andexpectContentType()withContentType.JSON, thenbuild(). - GET
/usersand apply the spec withthen().spec(). - Print the confirmation for users, then do the same for
/productsand/books, printing a confirmation after each.
Common mistakes
Calling spec() on the request side
given().spec()expects a request specification, so a response spec there does not compile. Response specs belong afterthen().Packing endpoint-specific checks into the shared spec
A spec that expects a
namefield fits users but not every collection. Keep shared specs to rules that hold for all responses, and add endpoint-specificbody()checks afterspec()in each test.Expecting a thrown exception to be returned
A failing spec throws
AssertionError, which stops the program at that line. There is no boolean result to test, so do not wrap the call in an if statement.
In a real test suite
Typical shared response specs check the success status, the JSON content type and a response-time ceiling, and sometimes a header every reply must carry. Error responses get their own spec, expecting the error status and an error field, which keeps negative tests just as short as positive ones.
Related lessons
- Combined Request and Response SpecsUses a response spec together with a request spec in one chain.
- Combined SpecificationRepeats status checks by hand that a response spec can absorb.
- Response Time ValidationAdds a response-time check, a common extra rule in shared specs.
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 io.restassured.http.ContentType;
import io.restassured.specification.ResponseSpecification;
import io.restassured.builder.ResponseSpecBuilder;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: Create a ResponseSpecification expecting status 200 and ContentType.JSON
// TODO: Validate /users, /products, and /books against this spec
// TODO: Print confirmation after each
}
}