About this lesson
REST Assured RequestSpecBuilder: a reusable base URI without global state
Learn REST AssuredIntermediateRequest Specification
How it works
Every lesson so far set the static RestAssured.baseURI field. That is convenient, but it is one shared value for the whole JVM, so two test classes that point at different services, or tests that run in parallel, can trip over each other. A request specification packs the settings into an object you pass around explicitly instead.
You create one with RequestSpecBuilder. Its setters, such as setBaseUri(), return the builder so they can be chained, and build() produces the RequestSpecification. Hand that object to given().spec() and every request in the chain starts from its settings. You then call the HTTP method with just the path, and REST Assured joins it to the base URI from the spec.
Two details are worth knowing. If neither the spec nor the global field provides a base URI, REST Assured falls back to its default of http://localhost, so the call goes to your own machine. And when both are set, the base URI from the spec wins over the global one, which lets one test override a project-wide default without touching it.
About the assignment
Without touching the global base URI, build a request specification for the practice API and use it to fetch user 1, reporting the name and city.
What you will do
- Create a
RequestSpecBuilder, callsetBaseUri()with the API root, thenbuild()to get aRequestSpecification. - Start a request with
given(), apply the spec withspec(), and GET/users/1using only the path. - Read
nameandcityfrom the response withjsonPath().getString(). - Print them after
Name:andCity:.
Common mistakes
Forgetting build()
The builder itself is not a specification. Passing the
RequestSpecBuildertospec()does not compile, because the method expects aRequestSpecification. End the chain withbuild().Leaving out the scheme
The base URI must start with
https://. Without a scheme the host cannot be worked out, and the request fails before it leaves the JVM, just as it does with the global field.Still setting the global field as well
Keeping
RestAssured.baseURIaround next to a spec hides which one a request really used. Pick the spec, remove the global assignment, and the test states its target in one place.
In a real test suite
In a JUnit 5 project the spec is usually built once in a @BeforeAll method or a small factory class and stored in a static field. The base URI then comes from a system property or environment variable, so the same tests can run against local, staging and production builds by changing one setting on the command line.
Related lessons
- Headers SpecificationAdds a default header to the same kind of builder.
- Combined SpecificationReuses one spec across three different endpoints.
- Your First REST Assured TestSets the global baseURI that this lesson replaces.
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.builder.RequestSpecBuilder;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
// TODO: Create a RequestSpecification with baseUri "https://testkru.com/api"
// TODO: Use given().spec(yourSpec).get("/users/1") to make the request
// TODO: Print name and city
}
}