Introduction to Restassured
What is Rest Assured?
Rest Assured is an open-source Java library designed specifically for testing RESTful APIs, using a readable, fluent syntax often described as "Given-When-Then."
Basic Example
given()
.baseUri("https://api.example.com")
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("Asha"));
Why It's Popular
Rest Assured integrates smoothly with Java testing frameworks like TestNG and JUnit, and its syntax closely mirrors how testers naturally describe API tests (given some setup, when a request is sent, then verify the response).
Adding Rest Assured to a Maven Project
Rest Assured is distributed through Maven Central under the group id io.rest-assured. Add it with test scope next to your test runner. Check Maven Central for the newest stable versions; the numbers here are only examples.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<!-- Optional: validate responses against a JSON Schema -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
Put your API test classes under src/test/java, keep shared settings such as the base URL in a base class or properties file, and run everything with mvn test so the same tests work on a CI server.
What You Can Verify With It
Rest Assured covers most of what an API tester checks day to day:
- Status codes such as 200, 201, 400, 401 and 404.
- Headers and cookies, for example
Content-Typeor caching headers. - Response body fields in JSON or XML, using path expressions and Hamcrest matchers.
- Response time, as a basic guard against slow endpoints.
- Response structure, by matching the body against a JSON Schema file.
- Authentication flows, including basic auth, bearer tokens and OAuth 2 headers.
Worked example: token, then protected call
Many APIs need a token before other endpoints can be called. The test below logs in, keeps the token, and uses it in a second request, then validates the response shape against a schema stored in src/test/resources.
import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import io.restassured.path.json.JsonPath;
String loginJson = given()
.baseUri("https://api.example.com")
.contentType("application/json")
.body("{\"email\":\"qa@example.com\",\"password\":\"secret\"}")
.when()
.post("/auth/login")
.then()
.statusCode(200)
.extract().asString();
String token = new JsonPath(loginJson).getString("token");
given()
.baseUri("https://api.example.com")
.auth().oauth2(token) // sends Authorization: Bearer <token>
.when()
.get("/orders")
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath("orders-schema.json"));
In a real project, read credentials from environment variables or a secrets store rather than writing them in the test.
Rest Assured Compared With Other API Tools
| Tool | Strength | Good for |
|---|---|---|
| Postman | Visual, quick to explore endpoints | Manual checks, sharing collections |
| Rest Assured | Plain Java code, version-controlled, CI-friendly | Automated regression in Java teams |
| Karate | Feature-file syntax with built-in assertions | Teams wanting less Java code |
| Python requests with pytest | Lightweight scripting | Python-based test stacks |
A common path is to explore an endpoint in Postman first, then turn the stable checks into Rest Assured tests that run on every build.
Practical Tips Before You Write Many Tests
- Test negative cases too: missing fields, wrong types, expired tokens and unknown ids.
- Keep each test independent by creating its own data and cleaning it up.
- Store request bodies as POJOs or files once payloads grow beyond a few fields.
- Point tests at a dedicated test environment, never at production data.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.