Skip to main content

๐Ÿงฑ Base Test Setup in RestAssured

๐Ÿ“˜ Introductionโ€‹

A base test setup is a foundational component of any API automation framework. It centralizes common configurations like RestAssured setup, logging, and authentication to ensure consistency across all tests.


โš™๏ธ 1. What Is a Base Test Class?โ€‹

A base test class is a parent class that all test classes extend.


๐Ÿ› ๏ธ 2. Setting Up RestAssured Globallyโ€‹

public class BaseTest {

@BeforeClass
public void setup() {
RestAssured.baseURI = "https://api.example.com";
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
}
}

๐Ÿ” 3. Adding Authenticationโ€‹

RestAssured.authentication = RestAssured.preemptive().basic("username", "password");

๐Ÿ“ฆ 4. Setting Up Preconditionsโ€‹

Response response = given()
.body("{ "name": "Test User" }")
.when()
.post("/users");

createdUserId = response.jsonPath().getString("id");

๐Ÿงน 5. Cleaning Up Resourcesโ€‹

@AfterClass
public void cleanup() {
given()
.when()
.delete("/users/" + createdUserId)
.then()
.statusCode(204);
}

โœ… Best Practicesโ€‹

  1. Centralize configuration
  2. Enable logging conditionally
  3. Handle edge cases
  4. Use environment variables

๐Ÿ Conclusionโ€‹

A well-designed base test setup ensures consistency, reduces duplication, and improves maintainability across your API automation framework.