-
Notifications
You must be signed in to change notification settings - Fork 0
JPostman Tutorial Part 2: Execute REST API Requests with RestAssured
-
Video tutorial: https://www.youtube.com/watch?v=hwsDjBSxIRs
In the previous tutorial, we created a Java Maven project, added jpostman-core, and loaded exported Postman collection and environment JSON files.
In this tutorial, we continue from that setup and execute REST API requests from Java using the JPostman RestAssured executor.
This page explains how to add the RestAssured adapter, configure logging, inspect a Postman request, resolve environment placeholders, and execute the request from Java.
By the end of this tutorial, you will know how to:
- Add the
jpostman-restassureddependency. - Add Logback to resolve SLF4J console warnings.
- Create a
logback-test.xmlconfiguration file. - Print the loaded Postman collection.
- Select a request from the collection by name.
- Print request details for debugging.
- Load and inspect the Postman environment.
- Build a request using environment values.
- Execute the request with
RestAssuredExecutor.
Before starting this tutorial, make sure you completed Part 1:
- Java Maven project is already created.
-
jpostman-coredependency is already added. - Exported Postman collection JSON file is in
src/test/resources. - Exported Postman environment JSON file is in
src/test/resources. - A basic TestNG test class already loads the collection and environment.
Open the existing Maven project in Eclipse.
Then open:
pom.xml
This is where the RestAssured adapter dependency will be added.
Right-click the pom.xml file, go to Maven, and select Add Dependency.
Add the JPostman RestAssured executor dependency:
<dependency>
<groupId>io.github.jpostman</groupId>
<artifactId>jpostman-restassured</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>As a best practice, use the test scope for testing-related libraries.
If you see an SLF4J warning in the console, add logback-classic as a test dependency.
Maven Central Repository - Logback
Example dependency:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>REPLACE_WITH_LATEST_VERSION</version>
<scope>test</scope>
</dependency>Use the latest version available from Maven Central.
Create a Logback configuration file under:
src/test/resources
File name:
logback-test.xml
Example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE" />
</root>
</configuration>This prints log messages to the console using the TRACE level.
To confirm the collection is loaded correctly, print the collection from the JPostman context.
Example:
cxt.getCollection().print();Run the test as a TestNG Test.
The console should show the collection requests and folders under the root level.
Import the JPostman Request class:
import io.jpostman.Request;Create a local Request variable:
Request req = cxt.getCollection().getRequest("Login user and get tokens");In this tutorial, the first POST request is selected by name:
Login user and get tokens
Print the selected request to inspect its details:
req.print();Before running the test, comment out the previous collection print statement so the console only shows the selected request details.
Example:
// cxt.getCollection().print();
Request req = cxt.getCollection().getRequest("Login user and get tokens");
req.print();Run the TestNG test again.
The console should display the request description and body. At this point, you may still see unresolved placeholders from the Postman environment.
Instead of hardcoding values in Java, read them from the exported Postman environment JSON file.
Import the JPostman Environment class:
import io.jpostman.Environment;Get the environment from the context:
Environment env = cxt.getEnvironment();
env.print();This shows the environment values that will replace placeholders in the request.
Example placeholders may include:
{{base_url}}
{{username}}
{{password}}
{{accessToken}}
Import the JPostman RestAssured executor:
import io.jpostman.restassured.RestAssuredExecutor;Build the request using the environment and execute it:
RestAssuredExecutor.execute(req.builder().build(env)).print();This resolves the environment placeholders, sends the REST API request, and prints the response.
A simplified test flow looks like this:
import org.testng.annotations.Test;
import io.jpostman.Environment;
import io.jpostman.JPostman;
import io.jpostman.JPostman.Context;
import io.jpostman.Request;
import io.jpostman.restassured.RestAssuredExecutor;
public class DemoTest {
@Test
public void test1() throws Exception {
Context cxt = JPostman
.fromResources()
.collection("DummyJSON.postman_collection.json")
.environment("DummyJSON.postman_environment.json")
.build();
Request req = cxt.getCollection().getRequest("Login user and get tokens");
Environment env = cxt.getEnvironment();
RestAssuredExecutor
.execute(req.builder().build(env))
.print();
}
}Update the collection and environment file names to match your exported JSON files.
To run the test:
- Save the Java test class.
- Right-click the test file.
- Select Run As.
- Choose TestNG Test.
You should see the REST API response printed in the console.
When the request is executed:
- JPostman loads the exported Postman collection.
- JPostman loads the exported Postman environment.
- The request is selected from the collection by name.
- Environment placeholders are resolved.
- The request is built.
-
RestAssuredExecutorsends the request. - The response is printed to the console.
Using the exported Postman environment avoids hardcoding values in Java.
Instead of changing Java code when API values change, you can update the values in Postman, export the environment again, and rerun the Java test.
This is useful for values such as:
- Base URL
- Username
- Password
- Access token
- Test-specific variables
In this tutorial, we used the existing JPostman Maven setup to execute a REST API request from Java.
We added the jpostman-restassured dependency, configured Logback logging, inspected the collection and request details, loaded environment variables, resolved placeholders, and executed the request with RestAssuredExecutor.
The next tutorial will continue with chained requests and additional JPostman features.
- Video tutorial: https://www.youtube.com/watch?v=hwsDjBSxIRs
- Part 1 video: https://www.youtube.com/watch?v=UxFjeONEq60
- JPostman GitHub repository: https://github.com/JPostman/jpostman
- Logback Classic on Maven Central: https://central.sonatype.com/artifact/ch.qos.logback/logback-classic