30/10/2024
API chaining is a technique that involves making a sequence of API requests where the response data from one API call is used as input for the next API call. This is particularly useful for scenarios where actions are dependent on the results of previous requests, such as creating a resource followed by retrieving or updating it.
Before we dive into API chaining, ensure you have REST Assured set up in your project. If you are using Maven, add the REST Assured dependency to your pom.xml
:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency>
Let’s consider an example where we want to:
First, we’ll write a method to create a new user. The response will include a userId
that we'll need for the next API call.
import io.restassured.RestAssured; import io.restassured.response.Response; public String createUser() { String requestBody = "{ \"name\": \"John Doe\", \"email\": \"john.doe@example.com\" }"; Response response = RestAssured.given() .contentType("application/json") .body(requestBody) .when() .post("https://api.example.com/users"); // Verify the response status response.then().statusCode(201); // Extract userId from the response return response.jsonPath().getString("userId"); }
Next, we retrieve the user details using the userId obtained from the previous step.
public void getUserDetails(String userId) { Response response = RestAssured.given() .pathParam("id", userId) .when() .get("https://api.example.com/users/{id}"); // Ensure the user is found response.then().statusCode(200); // Print user details System.out.println("User Details: " + response.asString()); }
Finally, update the user information by sending a PUT request.
public void updateUser(String userId) { String updateRequestBody = "{ \"name\": \"John Smith\", \"email\": \"john.smith@example.com\" }"; Response response = RestAssured.given() .contentType("application/json") .pathParam("id", userId) .body(updateRequestBody) .when() .put("https://api.example.com/users/{id}"); // Verify the update was successful response.then().statusCode(200); // Confirmation of update System.out.println("User updated successfully!"); }
Now, let’s chain these methods together to execute all three steps sequentially.
public void testApiChaining() { String userId = createUser(); // Step 1: Create User getUserDetails(userId); // Step 2: Get User Details updateUser(userId); // Step 3: Update User }
To run the chained API calls, simply invoke the testApiChaining
method from your main or test class.
public static void main(String[] args) { ApiChainingTest apiTest = new ApiChainingTest(); apiTest.testApiChaining(); }
API chaining allows for a streamlined process when interacting with RESTful services, where subsequent calls rely on the results of previous actions. By utilizing REST Assured, you can efficiently manage and test complex workflows in your API interactions.
By following the outlined steps and methods, you can modify and expand upon this example for your specific use case or testing scenarios.
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing