API Automation with REST Assured (Part 1)
REST Assured is a Java library for testing REST APIs: fluent syntax for requests and assertions. This post (Part 1) covers basics: sending GET and POST, asserting status and response body, and structuring your first API tests with JUnit or TestNG.
Why REST Assured?
- Fluent API: Readable request setup and assertions (e.g.
given().when().get().then().statusCode(200)). - Java: Fits Java shops; integrates with JUnit, TestNG, and Maven/Gradle.
- JSON/XML: Easy to assert on response body (path, fields, types).
- Auth, headers, cookies: Built-in support for common needs.
First request and assertion
Conceptually:
- Given: Base URL, headers (e.g. Content-Type, Authorization), body (for POST).
- When: Method and path (e.g. GET
/users/1). - Then: Assert status code, body (e.g.
body("id", equalTo(1))), headers.
You write this in Java with REST Assured's given(), when(), then() style.
Structure
- Base URL: Set once (e.g. in
@BeforeAllor config) so tests only specify path. - One test per scenario: e.g. get user positive, get user not found, create user positive.
- Reuse: Extract common given (auth, headers) into a method or filter.
Part 2 covers advanced assertions, JSON path, and organizing tests for CI.
Summary
- REST Assured = fluent Java API for HTTP requests and assertions; integrates with JUnit/TestNG.
- Use given/when/then to set up request and assert status and body.
- Set base URL and auth once; keep one test per scenario; Part 2 adds advanced assertions and structure.