APIs are the most testable surface in modern systems — fast, deterministic, and CI-friendly. This guide shows how to build a production-grade API testing stack using Postman + Newman for teams without heavy Java investment, and REST Assured for Java teams that need first-class IDE integration.
What to test in an API
- Status codes (2xx success, 4xx client, 5xx server).
- Payload shape (JSON Schema).
- Auth (Bearer, OAuth, API keys).
- Pagination + filters.
- Rate limits + retry behaviour.
- Edge cases (empty, oversize, unicode, malformed).
Postman: collections, environments, scripts
Postman is the de-facto tool for API exploration and team-shared collections. Use environments for base URLs + auth tokens, pre-request scripts for token refresh, and tests for assertions. Newman runs collections in CI without the GUI.
REST Assured: Java DSL
Contract testing with Pact
Pact verifies the consumer-provider agreement without integration tests. The consumer defines expectations; the provider verifies them in its own CI. Catches breaking API changes before they reach production.
Mock servers for isolation
- Postman Mock — built into Postman, easy to share with the team.
- WireMock — Java, programmable, advanced scenarios.
- MSW (Mock Service Worker) — JS, intercepts in-browser fetch calls.
Setting up Postman for a real API
A production-grade Postman workspace has three building blocks: a collection (group of requests), an environment (variables per dev/staging/prod), and pre-request + test scripts. Start by importing the OpenAPI spec — most teams publish one alongside their API.
- Collection structure — folder per resource; pre-request script for OAuth2 token refresh; test script with assertions on status, body, and headers.
- Environments — one file per environment (dev, staging, prod); secrets live in Postman's vault, not in the JSON export.
- Variables — collection, environment, and global scopes; `{{baseUrl}}/users/{{userId}}` lets you swap targets without changing tests.
- Pre-request scripts — refresh tokens, generate dynamic payloads, set trace IDs.
- Test scripts — `pm.test("returns 200", () => pm.response.to.have.status(200))` and JSON path assertions.
REST Assured for Java teams
REST Assured is a fluent DSL for Java API testing. It integrates cleanly with JUnit/TestNG and reads almost like English. The example below is a representative spec — a `GET /users/{id}` with auth and JSON assertions.
- Given — set request spec (auth, headers, body).
- When — fire the request (GET, POST, PUT, DELETE).
- Then — assert status, body, headers.
- Path params — `.get("/users/{id}", 42)` substitutes into the URL.
- JsonPath — `.body("name", equalTo("Ada"))` matches JSON nodes.
- Schema validation — `.body(matchesJsonSchemaInClasspath("user.json"))`.
Newman + CI for both stacks
Newman is the headless Postman runner. It integrates into CI the same way JUnit does: install, point at the collection, run. The same pattern works for REST Assured via `mvn test` or `gradle test`.
- Newman CLI — `newman run collection.json -e dev.json --reporters cli,htmlextra`.
- GitHub Actions — `docker://postman/newman_alpine33` runs the collection with no local install.
- JUnit/TestNG — standard test phase; reports in JUnit XML for CI dashboards.
- Reporting — Allure, ReportPortal, or Newman's HTML reporter. Store artefacts in CI for failed runs.
Schema validation, contract tests, and versioning
JSON Schema validation is the cheapest insurance against API regressions. Contract testing takes it further: the consumer and provider agree on a contract, and CI catches breaking changes before they ship.
- JSON Schema — define once, validate every response. `ajv` for Node, `networknt/json-schema-validator` for Java.
- Pact — consumer-driven contracts. Consumer publishes expectations; provider verifies them in its own CI.
- API versioning — `/v1`, `/v2` URL versioning is the most common; header versioning is cleaner but harder to debug.
- Breaking-change detection — `oasdiff` compares OpenAPI specs across versions and flags removed fields.
Common pitfalls and how to avoid them
Most API test failures come from one of three root causes. Address them up front and your suite stays green for years.
- Order-dependent tests — tests should run in any order. Use unique data per test (UUIDs) or test data factories.
- Hardcoded URLs — use environments + variables, not `localhost:3000` in the test code.
- Auth tokens that expire — automate token refresh in pre-request scripts or a setup hook.
- Skipping negative tests — test 4xx and 5xx explicitly. Assert the error shape, not just the status.
- Missing cleanup — delete created resources in an `afterEach` hook, or use a transactional sandbox.
FAQ
- Postman vs REST Assured — which first? Postman for non-Java teams and quick exploration; REST Assured for Java teams and IDE-integrated CI.
- Is Newman still maintained? Yes. Postman acquired Newman in 2017 and ships it as a first-class CLI runner.
- Can I test GraphQL with these tools? Yes — both support POST-based GraphQL queries. Schema validation is even more important with GraphQL.
- What about gRPC? Use `grpcui`, `ghz`, or Postman's experimental gRPC support. REST Assured does not cover gRPC natively.
Quick reference: REST Assured DSL cheat sheet
Keep this cheat sheet handy when writing REST Assured tests. It covers the 90% of assertions and request building you will use day-to-day.
- GET with query params — `.queryParam("page", 1).get("/users")`.
- POST with JSON body — `.contentType(ContentType.JSON).body(payload).post("/users")`.
- Status assertions — `.statusCode(200)`, `.statusLine("HTTP/1.1 200 OK")`.
- Header assertions — `.header("Content-Type", containsString("application/json"))`.
- Body path assertions — `.body("users[0].email", equalTo("[email protected]"))`.
- Response extraction — `.extract().jsonPath().get("token")`.
Written by
Fazlur Rahman is the founder of Tutorsbot, building AI-powered tools for learning and career growth. He writes about applying AI in real products and the practi… Read more
Fazlur Rahman is the founder of Tutorsbot, building AI-powered tools for learning and career growth. He writes about applying AI in real products and the practical side of building an ed-tech startup.








