Skip to content

@JPostmanTestContext

David Gofman edited this page Jun 22, 2026 · 3 revisions

Overview

@JPostmanTestContext injects the framework-specific JPostman test context into a test class.

Use this annotation to get a JUnitContext or TestNgContext that can be used by annotation-based tests and custom executor methods.

Basic TestNG Example

@JPostmanTestNG
public class DemoTest {

    @JPostmanTestContext
    private TestNgContext api;
}

Basic JUnit Example

@JPostmanJUnit(printFailures = true)
public class DemoJUnitTest {

    @JPostmanTestContext
    private JUnitContext api;
}

Namespace Example

Use a namespace when the test class works with more than one Postman context.

@JPostmanTestContext(namespace = "product")
private TestNgContext product;

@JPostmanTestContext(namespace = "user")
private TestNgContext user;

Then response annotations can target the matching namespace:

@JPostmanResponse(
    namespace = "product",
    folder = "Product",
    request = "Get all products"
)
@Test
public void getAllProducts() {
}

Configuration from Properties

@JPostmanTestContext can load configuration from jpostman.properties.

collection=classpath:DummyJSON.postman_collection.json
environment=classpath:DummyJSON.postman_environment.json
rules=classpath:demo_test_rule.ini

For a namespace:

collection.product=classpath:DummyJSON.product_collection.json
environment.product=classpath:DummyJSON.product_environment.json
rules.product=classpath:demo_test_rule.ini

Then use:

@JPostmanTestContext(namespace = "product")
private TestNgContext product;

Direct Configuration

You can also configure the context directly:

@JPostmanTestContext(
    namespace = "product",
    collection = "classpath:DummyJSON.postman_collection.json",
    environment = "classpath:DummyJSON.postman_environment.json",
    rules = "classpath:demo_test_rule.ini"
)
private TestNgContext product;

Working with the Injected Context

The injected context can be used in lifecycle methods, executor methods, and test methods.

@BeforeClass
public void beforeClass() {
    product.cache("started", true);
}
@JPostmanExecutor(name = "auth")
public ApiExecutor authExecutor(TestNgContext context) {
    return RestAssuredExecutor.apply(context.request())
            .auth()
            .oauth2(context.cache("token"));
}

Cache Reuse

The injected context is reused by the annotation runner so cache values can be shared between dependencies and tests.

Example:

product.cache("token", tokenValue);
String token = product.cache("token");

Common Pattern

@JPostmanTestNG
public class DemoTest {

    @JPostmanTestContext(namespace = "product")
    private TestNgContext product;

    @JPostmanResponse(
        namespace = "product",
        folder = "Product",
        request = "Get all products",
        log = true
    )
    @Test
    public void getAllProducts() {
        product.ctx().verify().print();
    }
}

Summary

@JPostmanTestContext injects the JUnit or TestNG context used by JPostman annotation tests. It is the main context object for request execution, response verification, caching, secure rules, and custom executors.

Clone this wiki locally