Introduction
RestAssured is a popular Java library used for automating REST API testing. It provides a domain-specific language for writing tests, making it easy to read and write tests.
Here’s a step-by-step guide to getting started with RestAssured:
Add RestAssured dependency to your project
You can add RestAssured as a dependency in your Maven or Gradle build file, or you can download the JAR file and add it to your project manually.
Here’s an example of adding RestAssured as a Maven dependency:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
Import the RestAssured library in your test class
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
Set the base URI for your API and Base Path
RestAssured.baseURI = “https://api.example.com”;
RestAssured.basePath = “/v1”;
Send HTTP requests and validate responses
RestAssured provides a fluent API for sending HTTP requests and validating responses. You can use methods such as given()
, when()
, and then()
to structure your tests.
Here’s an example of sending a GET request to retrieve a list of users and validating the response:
@Test
public void testGetUsers() {
given()
.queryParam(“status”, “active”)
.when()
.get(“/users”)
.then()
.statusCode(200)
.body(“size()”, greaterThan(0))
.body(“get(0).name”, equalTo(“John”))
.body(“get(0).age”, equalTo(30));
}
In this example, we’re sending a GET request to the /users
endpoint with a query parameter of status=active
. We’re using the statusCode()
method to assert that the response code is 200, and the body()
method to assert that the response body contains at least one user with the name “John” and age 30.
Here are some other examples of RestAssured methods you can use:
given().contentType(ContentType.JSON).body(payload).when().post("/users").then().statusCode(201)
– sending a POST request with a JSON payload and asserting that the response code is 201 (Created).given().header("Authorization", "Bearer " + token).when().get("/profile").then().statusCode(200).body("email", equalTo("user@example.com"))
– sending a GET request with an authorization header and asserting that the response code is 200 and the email in the response body is “user@example.com“.
Use RestAssured to test API endpoints with different HTTP methods and content types
RestAssured supports various HTTP methods such as GET, POST, PUT, DELETE, etc. You can use the .get()
, .post()
, .put()
, .delete()
methods to send requests with different methods.
Here’s an example of sending a POST request with a JSON payload:
@Test
public void testCreateUser() {
String payload = “{\”name\”: \”John\”, \”age\”: 30}”;
given()
.contentType(ContentType.JSON)
.body(payload)
.when()
.post(“/users”)
.then()
.statusCode(201)
.body(“name”, equalTo(“John”))
.body(“age”, equalTo(30));
}
In this example, we’re sending a POST request to the /users
endpoint with a JSON payload containing the name and age of a user. We’re using the contentType()
method to set the content type to JSON, and the body()
method to set the request body to the JSON payload.
We’re then using the statusCode()
method to assert that the response code is 201 (Created), and the body()
method to assert that the response body contains the name and age of the user we just created.
You can replace the payload with your own JSON data, and replace the endpoint with the endpoint of your own API.