Open In App

How to Test API with REST Assured?

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, easy-to-maintain tests for RESTful APIs. It allows you to specify the expectations for HTTP responses from a RESTful API, and it integrates seamlessly with JUnit, the most popular testing framework for Java. REST Assured provides a simple, fluent API for specifying the expectations for HTTP responses. 

Steps to Test API with REST Assured

  1. Install the REST Assured library. REST Assured is a Java library, so you will need to add it to your project as a dependency. This typically involves adding a line to your “pom.xml” file if you are using Maven, or adding the REST Assured JAR file to your classpath if you are using a different build system.
  2. Import the REST Assured classes that you need. In order to use REST Assured, you will need to import the relevant classes from the “io.restassured” package. For example, you will typically need to import the “RestAssured” class, as well as any classes that you need for specifying the expectations for the API response.
  3. Create a test class. REST Assured tests are typically JUnit tests, so you will need to create a JUnit test class to contain your tests. This class should be annotated with the @Test annotation, and it should contain one or more test methods that make requests to the API and specify the expectations for the responses.
  4. Make a request to the API. To test the API, you will need to make a request to one of its endpoints. REST Assured provides a number of methods for making different types of requests, such as get() for making GET requests and post() for making POST requests. You can use these methods to make a request to the API, and then use the then() method to specify your expectations for the response.
  5. Specify your expectations for the response. After making a request to the API, you can use the then() method to specify your expectations for the response. REST Assured provides a number of methods that you can use to specify these expectations, such as statusCode() for specifying the expected status code, contentType() for specifying the expected content type, and body() for specifying the expected body.

Here is an example of how you might test an API with REST Assured:

Java




import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.Test;
  
import static org.hamcrest.Matchers.equalTo;
  
public class MyApiTest {
  
  @Test
  public void testGet() {
    RestAssured
        .get("http://my-api.com/resource/123")
        .then()
        .assertThat()
        .statusCode(200)
        .and()
        .contentType(ContentType.JSON)
        .and()
        .body("id", equalTo(123));
  }
  
}


In this example, we are using REST Assured to make a GET request to the http://my-api.com/resource/123 endpoint, and then we are using the then() method to specify our expectations for the response. In this case, we are expecting the response to have a 200 status code, to have a Content-Type of application/json, and to have a JSON body with an id field that has a value of 123. Once you have written your tests, you can run them using JUnit, either from your IDE or from the command line. If any of your expectations are not met, the test will fail, and you will see an error message indicating what went wrong.

In addition to the GET method shown above, REST Assured also provides methods for making other types of requests, such as POST, PUT, DELETE, and so on. You can also use REST Assured to specify expectations for the response headers, cookies, and other aspects of the HTTP response. For more information, see the REST Assured documentation. To test an API with REST Assured, you will first need to download the REST Assured library and include it in your project. Then, you can use the library to create a RestAssured object, which you can use to send HTTP requests to the API and verify the response.

Here is an example of how you might test an API using REST Assured:

Java




import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.Test;
  
public class MyApiTest {
  
    @Test
    public void testGet() {
        RestAssured.baseURI = "http://myapi.com";
  
        // Send a GET request to the API
        Response response = RestAssured.get("/api/resource");
  
        // Verify that the response has a 200 OK status code
        response.then().assertThat().statusCode(200);
  
        // Verify that the response body contains the expected data
        response.then().assertThat().body("some.property", equalTo("expected value"));
    }
}


In this example, we use the get() method of the RestAssured object to send a GET request to the API, and then we use the then() and assertThat() methods to verify that the response has the expected status code and body. You can use similar methods to test other HTTP methods, such as post() for POST requests, put() for PUT requests, and delete() for DELETE requests.

It’s important to note that this is just a brief overview of how to test an API with REST Assured, and there are many other features and options available in the library that you can use to test your API more thoroughly. For more information, you can refer to the REST Assured documentation.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads