Skip to content

@JPostmanRequest

David Gofman edited this page Jun 22, 2026 · 1 revision

Overview

@JPostmanRequest prepares a Postman request before a dependency method or test method runs.

It is useful when a method needs to prepare a request, execute custom code, return a cached value, or set up data for another response.

Basic Example

@JPostmanRequest(request = "Login user and get tokens")
public String getToken() {
    return api.response(c -> RestAssuredExecutor.execute(c.request()))
            .asserts(true)
            .exists("accessToken", "Access token not found")
            .verify()
            .path("accessToken");
}

Dependency Example

The returned value is cached by dependency name unless cache is defined.

@JPostmanRequest(request = "Login user and get tokens")
public String getToken() {
    return api.response(c -> RestAssuredExecutor.execute(c.request()))
            .verify()
            .path("accessToken");
}

@JPostmanExecutor(name = "auth", dependsOn = "getToken")
public ApiExecutor authExecutor(TestNgContext context) {
    return RestAssuredExecutor.apply(context.request())
            .auth()
            .oauth2(context.cache("getToken"));
}

Custom Cache Key

Use cache to store the dependency result under a specific key.

@JPostmanRequest(
    request = "Login user and get tokens",
    cache = "token"
)
public String getToken() {
    return api.response(c -> RestAssuredExecutor.execute(c.request()))
            .verify()
            .path("accessToken");
}

Then use:

context.cache("token")

Namespace and Folder

@JPostmanRequest(
    namespace = "product",
    folder = "Product",
    request = "Get all products"
)
public void prepareProductRequest() {
}
  • namespace selects the configured JPostman context.
  • folder selects the Postman folder.
  • request selects the request inside the collection or folder.

Secure Rule Section

Use rule to load a secure rule profile before preparing the request.

@JPostmanRequest(
    namespace = "product",
    folder = "Product",
    request = "Get all products",
    rule = "product"
)
public void prepareProductRequest() {
}

Depends On

dependsOn runs dependency methods before this method.

@JPostmanRequest(
    request = "Get current auth user",
    dependsOn = "getToken"
)
public void prepareAuthUser() {
}

Void Dependencies

A void dependency can be used for setup-only work.

@JPostmanRequest(request = "Setup request")
public void setupRequest() {
    api.cache("ready", true);
}

If another request needs a value, return the value instead of using void.

Good token dependency:

@JPostmanRequest(request = "Login user and get tokens", cache = "token")
public String getToken() {
    return api.response(c -> RestAssuredExecutor.execute(c.request()))
            .verify()
            .path("accessToken");
}

Full TestNG Example

@JPostmanTestNG
public class DemoTest {

    @JPostmanTestContext
    private TestNgContext api;

    @JPostmanRequest(request = "Login user and get tokens", cache = "token")
    public String getToken() {
        return api.response(c -> RestAssuredExecutor.execute(c.request()))
                .verify()
                .path("accessToken");
    }

    @JPostmanExecutor(name = "auth", dependsOn = "getToken")
    public ApiExecutor authExecutor(TestNgContext context) {
        return RestAssuredExecutor.apply(context.request())
                .auth()
                .oauth2(context.cache("token"));
    }
}

Summary

@JPostmanRequest prepares a request and can run custom method code. It is especially useful for dependencies that produce cached values, such as login tokens.

Clone this wiki locally