#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Rest Assured

Setting Up Common Specification For The Same Kind Of Requests Using Restassured

What is a Request Specification?

A RequestSpecification bundles common configuration — base URI, headers, content type — into a single reusable object, avoiding repetition across many tests.

Example

RequestSpecification spec = new RequestSpecBuilder()
    .setBaseUri("https://api.example.com")
    .addHeader("Authorization", "Bearer " + token)
    .setContentType("application/json")
    .build();

given().spec(spec).when().get("/users").then().statusCode(200);

Why This Matters

Centralizing shared configuration in one specification makes the whole test suite easier to maintain — a single change (like a new auth header) only needs to be made in one place.

Pairing Request and Response Specifications

A request specification handles what you send. A ResponseSpecification does the same job for what you expect back, so common checks are not repeated in every test.

ResponseSpecification okJson = new ResponseSpecBuilder()
        .expectStatusCode(200)
        .expectContentType(ContentType.JSON)
        .expectResponseTime(lessThan(3000L))
        .build();

given().spec(spec)
.when().get("/users")
.then().spec(okJson)
       .body("data", not(empty()));

You can still add test-specific assertions after applying the shared spec, as the body check above shows.

Organising Specs in a Base Class

Most frameworks build specifications once, in a base test class or a small factory, and let every test class reuse them.

public abstract class BaseApiTest {

    protected static RequestSpecification reqSpec;
    protected static ResponseSpecification okSpec;

    @BeforeClass
    public static void setUpSpecs() {
        String baseUri = System.getProperty("baseUri", "https://api.example.com");
        String token   = System.getenv("API_TOKEN");

        reqSpec = new RequestSpecBuilder()
                .setBaseUri(baseUri)
                .setBasePath("/v1")
                .addHeader("Authorization", "Bearer " + token)
                .setContentType(ContentType.JSON)
                .addFilter(new RequestLoggingFilter())
                .addFilter(new ResponseLoggingFilter())
                .build();

        okSpec = new ResponseSpecBuilder()
                .expectStatusCode(200)
                .build();
    }
}

Reading the base URI and token from system properties and environment variables means the same suite can run against QA, staging or a local server without code changes, and secrets stay out of the repository.

Rest Assured also lets you set global defaults with RestAssured.requestSpecification = reqSpec;. That is convenient for small projects, but explicit .spec() calls make it clearer which configuration a test uses.

Extending a Shared Spec for Special Cases

Some endpoints need extra settings, such as a different content type for file uploads. Rather than copying the whole builder, start from the shared spec and add to it.

RequestSpecification uploadSpec = new RequestSpecBuilder()
        .addRequestSpecification(reqSpec)
        .setContentType("multipart/form-data")
        .build();

Common Mistakes

  • Hard-coding tokens inside the specification and committing them to Git.
  • Building a spec with a token that expires midway through a long run; refresh it or use an authentication filter.
  • Logging full requests in CI when they contain passwords or tokens; log only on failure with log().ifValidationFails().
  • Putting endpoint-specific paths into the shared spec, which makes it less reusable.
  • Sharing a mutable spec across parallel threads and changing it inside tests; build a new one instead.

Designing Specs for Several Services

Many projects test more than one API, such as a user service and a payment service, each with its own base URL and authentication. Instead of one giant specification, create a small factory class with a clearly named method per service, like Specs.userService() and Specs.paymentService(). Each method builds and returns a fresh specification with only the settings that service needs.

This keeps tests readable, because a test shows at a glance which service it calls. It also limits the impact of change: if the payment team switches to a new authentication header, you update one method, and every payment test picks it up. When reviewing pull requests, check that new tests reuse an existing spec instead of quietly building their own, which is how duplication creeps back in.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course