Skip to content

JPostman Tutorial Part 2: Execute REST API Requests with RestAssured

David Gofman edited this page Jun 15, 2026 · 4 revisions

Execute REST API Requests with RestAssured

Overview

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.

What You Will Learn

By the end of this tutorial, you will know how to:

  • Add the jpostman-restassured dependency.
  • Add Logback to resolve SLF4J console warnings.
  • Create a logback-test.xml configuration 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.

Prerequisites

Before starting this tutorial, make sure you completed Part 1:

  • Java Maven project is already created.
  • jpostman-core dependency 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.

Step 1: Review the Current Maven Project

Open the existing Maven project in Eclipse.

Then open:

pom.xml

This is where the RestAssured adapter dependency will be added.

Step 2: Add the JPostman RestAssured Dependency

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.

Step 3: Add Logback for Console Logging

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.

Step 4: Create logback-test.xml

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.

Step 5: Print the Loaded Collection

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.

Step 6: Select a Request from the Collection

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

Step 7: Print the Selected Request

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.

Step 8: Load and Print the 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}}

Step 9: Execute the Request with RestAssuredExecutor

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.

Example Test Flow

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.

Step 10: Run the Test

To run the test:

  1. Save the Java test class.
  2. Right-click the test file.
  3. Select Run As.
  4. Choose TestNG Test.

You should see the REST API response printed in the console.

What Happens During Execution

When the request is executed:

  1. JPostman loads the exported Postman collection.
  2. JPostman loads the exported Postman environment.
  3. The request is selected from the collection by name.
  4. Environment placeholders are resolved.
  5. The request is built.
  6. RestAssuredExecutor sends the request.
  7. The response is printed to the console.

Why Use the Postman Environment?

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

Summary

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.

Related Links

Clone this wiki locally