-
Notifications
You must be signed in to change notification settings - Fork 0
@JPostmanTestContext
@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.
@JPostmanTestNG
public class DemoTest {
@JPostmanTestContext
private TestNgContext api;
}@JPostmanJUnit(printFailures = true)
public class DemoJUnitTest {
@JPostmanTestContext
private JUnitContext api;
}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() {
}@JPostmanTestContext can load configuration from jpostman.properties.
collection=classpath:DummyJSON.postman_collection.json
environment=classpath:DummyJSON.postman_environment.json
rules=classpath:demo_test_rule.iniFor a namespace:
collection.product=classpath:DummyJSON.product_collection.json
environment.product=classpath:DummyJSON.product_environment.json
rules.product=classpath:demo_test_rule.iniThen use:
@JPostmanTestContext(namespace = "product")
private TestNgContext product;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;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"));
}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");@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();
}
}@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.