REST ASSURED framework tutorial: concise guide
Rest Assured framework tutorial: concise guide
REST ASSURED is a powerful Java library used to perform and verify the API test based on the RESTFUL architecture.This article will provide you with a concise guide for the REST ASSURD framework, helping you get started and familiarize with its main functions and usage.
1. Introduce REST Assured framework
First of all, you need to introduce REST assured dependence in the Java project.Add the following dependencies to the Maven or Gradle Construction file of your project:
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
Gradle:
groovy
testCompile 'io.rest-assured:rest-assured:4.3.0'
2. Send HTTP request
REST ASSURD provides a simple and easy -to -use API to send HTTP requests.Here are an example of sending GET requests:
import io.restassured.RestAssured;
import io.restassured.response.Response;
public class APITest {
public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
int statusCode = response.getStatusCode();
String responseBody = response.getBody().asString();
System.out.println("Response Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
}
}
The above code sends a get request to the "https://api.example.com/users" address, and obtain the status code and the response body from the response.
3. Verification response
REST Assured also provides a series of assertions for verifying response.The following is an example of verifying the response status code and response:
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class APITest {
public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
given().when().get("https://api.example.com/users").then()
.statusCode(200)
.body("users.name", hasItems("Alice", "Bob"));
}
}
The above code uses the GIVEN-WHEN-THEN style of REST ASSURED, and uses an assertion method to verify the status code and responding body.
4. Send other types of HTTP requests
In addition to GET requests, REST ASSURED also supports sending other types of HTTP requests, such as Post, PUT, and Delete.The following is an example of sending post requests:
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
public class APITest {
public static void main(String[] args) {
String requestBody = "{\"name\":\"Alice\", \"age\":25}";
Response response = RestAssured.given()
.contentType("application/json")
.body(requestBody)
.post("https://api.example.com/users");
int statusCode = response.getStatusCode();
String responseBody = response.getBody().asString();
System.out.println("Response Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
}
}
The above code sends a post request to the "https://api.example.com/users" address, and it comes with a JSON format request body.
Through this concise guide, you should now have a basic understanding of the use of the Rest Assured framework to send and verify the HTTP request.I hope this article can help you be more efficient and accurate when performing API tests.