Skip to content
CodeKruPro

Base URI Specification

39/62

Base URI Specification

Instead of setting a global RestAssured.baseURI, you can create a reusable RequestSpecification:

RequestSpecification spec = new RequestSpecBuilder()
    .setBaseUri("https://testkru.com/api")
    .build();

Then pass it to given() with .spec(spec) and call the HTTP method with just the path, for example /products/1. The base URI comes from the spec.

This is the recommended pattern for production tests — it's reusable, composable, and avoids global state.

Task: Create a spec and use it to fetch user 1.

Expected output:

Name: John Doe
City: New York

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

  1. Create a RequestSpecBuilder, call setBaseUri() with the API root, then build() to get a RequestSpecification.
  2. Start a request with given(), apply the spec with spec(), and GET /users/1 using only the path.
  3. Read name and city from the response with jsonPath().getString().
  4. Print them after Name: and City:.

Common mistakes

  • Forgetting build()

    The builder itself is not a specification. Passing the RequestSpecBuilder to spec() does not compile, because the method expects a RequestSpecification. End the chain with build().

  • 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.baseURI around 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.

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.

Main.java
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
    }
}