,false,false]–> test org.testng testng 7.7.0 test Use code with caution. 2. Write Your First GET Test
The following example sends a GET request to a sample API and validates the response code.
import org.testng.annotations.Test; import static io.rest-assured.RestAssured.; import static org.hamcrest.Matchers.; public class FirstAPITest { @Test public void getExample() { given() .baseUri(”https://typicode.com”) .when() .get(“/posts/1”) .then() .statusCode(200) // Validate status code .body(“id”, equalTo(1)) // Validate body content .log().all(); // Log the response } } Use code with caution. 3. Understanding the Syntax
Given(): Pre-requisites, such as headers, cookies, base URIs, or request parameters. When(): The HTTP method (GET, POST, etc.) and the endpoint.
Then(): Assertions, such as status code, body validation, or response time validation. Key Concepts to Learn
Handling JSON/XML Response: Parsing data within the response body to validate specific fields. Request Parameters & Headers: Sending data in the request.
POST Request Payloads: Sending data (JSON/XML) in the request body.
Logging: Using .log().all() to debug requests and responses.
Assertions: Using Hamcrest matchers (e.g., equalTo, hasItems). Conclusion
REST Assured is a powerful, straightforward, and highly effective tool for API automation. Its BDD-style syntax reduces the learning curve, enabling QA engineers and developers to build robust API test suites quickly. By following this guide, you can start automating API testing in your Java projects today.
If you are just getting started, I can help you by explaining how to handle authentication or how to create a more advanced request body (like a POST request with a JSON object). Let me know what you need next!
Introduction to Rest Assured: A Beginner’s Guide to API Testing