</>

Technology

Rest Assured

Difficulty

Beginner

Interview Question

How to set baseURI, basePath, and basePort in Rest Assured?

Answer

BaseURI, BasePath, and BasePort in Rest Assured

These static fields allow you to configure the base URL globally so you don''t need to repeat it in every test.

Setting Global Defaults

Java
import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;

public class BaseTest {

    @BeforeClass
    public void setupRestAssured() {

        // Base URI — the scheme and host
        RestAssured.baseURI = "https://reqres.in";

        // Base Path — common path prefix
        RestAssured.basePath = "/api";

        // Base Port — if not default (80/443)
        RestAssured.port = 8080; // only needed for non-standard ports

        // SSL relaxation (for test environments with self-signed certs)
        RestAssured.useRelaxedHTTPSValidation();
    }
}

Result: Every request automatically uses https://reqres.in:8080/api as the prefix.

Using Without Repeating Base URL

Java
@Test
public void testGetUser() {
    // Full URL: https://reqres.in/api/users/2
    given()
        .when()
        .get("/users/2")   // ← only relative path needed
        .then()
        .statusCode(200);
}

@Test
public void testCreateUser() {
    // Full URL: https://reqres.in/api/users
    given()
        .body("{ \"name\": \"John\" }")
        .contentType(ContentType.JSON)
        .when()
        .post("/users")    // ← only relative path
        .then()
        .statusCode(201);
}

Environment-Based Configuration

Java
// config.properties
base.url=https://api.staging.com
base.path=/api/v2
port=443

// BaseTest.java
@BeforeClass
public void setup() {
    RestAssured.baseURI  = ConfigManager.get("base.url");
    RestAssured.basePath = ConfigManager.get("base.path");
    RestAssured.port     = Integer.parseInt(ConfigManager.get("port"));
}

Resetting Defaults

Java
@AfterClass
public void teardown() {
    RestAssured.reset(); // Resets all static defaults
}

Summary

ConfigExampleEffect
baseURIhttps://api.example.comScheme + host
basePath/api/v1Common URL prefix
port8080Non-default port
useRelaxedHTTPSValidation()Skip SSL cert check

Follow AutomateQA

Related Topics