Rest Assured Introduction - Notes By ShariqSP
Introduction to Rest Assured
Rest Assured is a popular open-source Java library designed for testing RESTful APIs. It provides a simple, domain-specific language (DSL) that makes it easy to send HTTP requests and validate responses. Rest Assured is widely used in API testing for its ease of use, flexibility, and integration with testing frameworks like JUnit and TestNG. This library allows testers and developers to automate their API testing, enabling faster and more efficient test cycles.
Why Do We Need Rest Assured?
APIs are the backbone of modern applications, facilitating communication between various services. Ensuring the reliability of these APIs is crucial, especially for business-critical services like payment gateways. Manual testing tools like Postman are great for exploratory testing, but for automated regression testing and integration into CI/CD pipelines, a programmatic approach using Rest Assured is more effective. Rest Assured reduces the overhead of writing boilerplate code, making it easier to perform comprehensive API testing with minimal effort.
Scenario: Testing Amazon Payment Gateway API
Consider an e-commerce application that integrates with Amazon's payment gateway to process customer transactions. The payment gateway exposes an endpoint /api/payment/authorize
that accepts a POST
request with transaction details like customer ID, order amount, and payment method. As part of API testing, we need to ensure that the payment authorization process works correctly and responds with appropriate status codes and messages.
Without Rest Assured, you would need to use HttpURLConnection
or similar libraries, which can involve writing a lot of boilerplate code for setting up the request, handling headers, parsing JSON responses, and managing exceptions. Rest Assured streamlines this process, allowing you to focus on the test logic.
Example: Using Rest Assured to Test Amazon Payment Gateway API
// Import Rest Assured
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import org.testng.annotations.Test;
public class AmazonPaymentGatewayTest {
@Test
public void testPaymentAuthorization() {
given()
.baseUri("https://payments.amazon.com")
.basePath("/api/payment/authorize")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer your-api-token")
.body("{ \"customerId\": \"C12345\", \"amount\": 150.00, \"currency\": \"USD\", \"paymentMethod\": \"CreditCard\", \"orderId\": \"ORD98765\" }")
.when()
.post()
.then()
.statusCode(200)
.body("status", equalTo("Authorized"))
.body("transactionId", notNullValue())
.body("message", equalTo("Payment authorized successfully"));
}
}
In this example, Rest Assured makes it straightforward to set up the HTTP request, send it, and validate the response. We check that the payment authorization endpoint returns a status code of 200
and a response body indicating that the payment was authorized. The use of fluent syntax helps in creating readable and maintainable tests.
Benefits of Using Rest Assured
- Reduces boilerplate code for HTTP requests and response handling.
- Supports validation of response headers, status codes, and body content.
- Integrates seamlessly with CI/CD pipelines for automated testing.
- Provides better readability with its fluent API syntax.