# REST Assured — Fluent Java API Testing for RESTful Services > REST Assured is a Java library for testing REST APIs with a fluent, BDD-style syntax. It simplifies HTTP request construction, response validation, and JSON/XML assertion, making API tests readable and concise in Java and Kotlin projects. ## Install Save as a script file and run: # REST Assured — Fluent Java API Testing for RESTful Services ## Quick Use ```xml io.rest-assured rest-assured 5.5.0 test ``` ```java import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; given(). queryParam("page", 1). when(). get("/api/users"). then(). statusCode(200). body("data.size()", greaterThan(0)); ``` ## Introduction REST Assured is a Java DSL for simplifying the testing of REST-based APIs. It brings the ease of dynamic-language HTTP testing to Java by providing a fluent given/when/then syntax for constructing requests and validating responses. It is widely used in enterprise Java projects for both unit and integration API testing. ## What REST Assured Does - Sends HTTP requests (GET, POST, PUT, DELETE, PATCH) with a readable fluent API - Validates response status codes, headers, cookies, and body content in a single chain - Parses and asserts JSON and XML response bodies using GPath and JsonPath expressions - Supports authentication schemes including Basic, Digest, OAuth 1/2, and custom headers - Integrates with JUnit, TestNG, and assertion libraries like Hamcrest and AssertJ ## Architecture Overview REST Assured wraps Apache HttpClient to handle HTTP communication and provides a specification builder that accumulates request parameters (headers, query params, body, auth) through method chaining. The response object exposes a rich API for extracting and asserting values. JSON validation uses Groovy's GPath syntax via an embedded JsonPath engine, while XML uses XmlPath. The library ships as a single jar with optional modules for JSON Schema validation, Kotlin extensions, and Spring MockMvc integration. ## Self-Hosting & Configuration - Add the Maven or Gradle dependency to your test scope - Set a base URI globally with `RestAssured.baseURI = "http://localhost:8080"` in a setup method - Configure default headers, authentication, and content type via `RestAssured.config` - Use request and response specifications to avoid repeating common setup across tests - Enable logging with `.log().all()` on requests or responses for debugging ## Key Features - BDD-style given/when/then syntax for readable and self-documenting API tests - JsonPath and XmlPath for deep assertions on nested response structures - JSON Schema validation to verify response structure matches a contract - Spring MockMvc integration for testing Spring Boot controllers without a running server - Kotlin extensions module for even more idiomatic test code ## Comparison with Similar Tools - **HTTPie / curl** — command-line tools for manual testing; not integrated into test suites - **Postman / Newman** — GUI-first API testing with collection runners; less natural for Java CI pipelines - **Spring WebTestClient** — reactive testing client for Spring WebFlux; Spring-specific - **Karate** — BDD API testing with its own DSL in feature files; less Java-native - **WireMock** — API mocking and stubbing; complementary to REST Assured for service virtualization ## FAQ **Q: Can I use REST Assured with Kotlin?** A: Yes. There is a dedicated Kotlin extensions module that provides infix functions for even cleaner syntax. **Q: Does it support multipart file uploads?** A: Yes. Use `.multiPart(file)` in the request specification to upload files. **Q: How do I validate JSON Schema?** A: Add the `rest-assured-json-schema-validator` module and use `.body(matchesJsonSchemaInClasspath("schema.json"))`. **Q: Can I test SOAP or GraphQL APIs?** A: REST Assured is designed for REST APIs but can send arbitrary HTTP requests, so GraphQL queries via POST work fine. For SOAP, dedicated libraries like Apache CXF are better suited. ## Sources - https://github.com/rest-assured/rest-assured - https://rest-assured.io/ --- Source: https://tokrepo.com/en/workflows/asset-8ad83f90 Author: Script Depot