Java Academy

Master Java Development

Menu

Core Java

Servlets

JDBC

Microservices

Build Tools

APIs

APIs · Lesson 12

API Testing with Postman

Why Test APIs with Postman

Postman lets developers send HTTP requests, inspect responses, and save repeatable examples. It is useful for exploratory checks and collaboration, while automated integration tests protect behavior in CI. A collection should describe meaningful workflows rather than only happy paths.

Modeling an API Check

A test can assert both the status and important response fields. The Java example mirrors the same assertions an automated HTTP client would make.

public class ApiContractCheck {
    public static void assertCourseResponse(int statusCode, String responseBody) {
        if (statusCode != 200) {
            throw new AssertionError("Expected 200 but received " + statusCode);
        }
        if (!responseBody.contains("Servlet Fundamentals")) {
            throw new AssertionError("Course title was not returned");
        }
    }

    public static void main(String[] arguments) {
        assertCourseResponse(200, "{\"title\":\"Servlet Fundamentals\"}");
        System.out.println("Contract check passed");
    }
}

Useful Test Cases

CaseExpected resultWhy
Valid requestSuccess responseConfirms normal behavior
Missing field400Protects validation contract
Unauthenticated401Protects security boundary
Unknown resource404Clarifies lookup behavior
Dependency failure5xx or documented fallbackTests resilience

Common Mistake and Practices

Common Mistake: Saving a Postman request that depends on a developer's local token or URL. Other developers and CI cannot run it reliably.

  • Use environment variables for hosts and tokens.
  • Keep secrets out of exported collections.
  • Test status, headers, and important body fields.
  • Include negative and authorization cases.
  • Run collection checks in CI when practical.

Real-World Use Case

An API collection tests course creation, duplicate enrollment, and unauthorized lesson publishing. The same requests help a developer explore the service locally and verify a release in CI.

Quiz Check

Quick Knowledge Test

Loading quiz questions...