NoteTube

Session 2: API Testing | RestAssured | Creating Post Request Payloads in Multiple Ways
1:00:27

Session 2: API Testing | RestAssured | Creating Post Request Payloads in Multiple Ways

SDET- QA

6 chapters7 takeaways12 key terms5 questions

Overview

This video demonstrates four distinct methods for creating request bodies in API testing using RestAssured, specifically focusing on POST requests. The presenter explains and provides code examples for using HashMaps, the org.json library, POJO (Plain Old Java Object) classes, and external JSON files. Each method is illustrated with a practical example, including setting up a local JSON server for testing and performing subsequent delete operations. The video emphasizes the importance of choosing the right approach based on the complexity and source of the data.

How was this?

Save this permanently with flashcards, quizzes, and AI chat

Chapters

  • POST requests require a request body to be sent along with the request.
  • There are four primary ways to create this request body: HashMap, org.json library, POJO class, and external JSON file.
  • The video will demonstrate each method using a student API for practical examples.
  • A local JSON server will be set up using `json-server` to simulate the API.
Understanding these different methods allows testers to flexibly construct request bodies based on data availability and project conventions, ensuring accurate API interactions.
Setting up a local student API by running `json-server students.json` in the command prompt.
  • HashMaps store data in key-value pairs, which aligns well with JSON structure.
  • For nested structures like arrays (e.g., 'courses'), a Java array is created first and then added as a value to the HashMap.
  • The HashMap data can be directly passed to the `body()` method in RestAssured.
  • This method is suitable for small, simple data sets.
HashMaps offer a straightforward way to represent simple JSON structures directly within Java code, making it easy to construct and send basic POST request payloads.
Creating a HashMap with keys like 'name', 'location', 'phone', and 'courses', where 'courses' holds a String array like `new String[]{"C", "C++"}`.
  • This method requires adding the `org.json` dependency to the `pom.xml` file.
  • A `JSONObject` is created and populated using the `put()` method, similar to HashMap.
  • Nested arrays are handled by creating Java arrays first.
  • Crucially, the `JSONObject` must be converted to a String using `.toString()` before being passed to the `body()` method.
The `org.json` library provides a dedicated JSON object structure, which can be more explicit for JSON manipulation, though it requires an extra step of string conversion for the request body.
Instantiating `JSONObject data = new JSONObject();` and then using `data.put("name", "Scott");` and `data.put("courses", coursesArray);`, followed by `body(data.toString())`.
  • POJO (Plain Old Java Object) classes encapsulate data with private fields and public getter/setter methods.
  • A separate POJO class is created with fields corresponding to the JSON structure (e.g., name, location, phone, courses).
  • Getters and setters are automatically generated using IDE features.
  • An instance of the POJO class is created, and its setters are used to populate data, which is then passed to the `body()` method.
Using POJO classes promotes code organization, reusability, and maintainability, especially for complex or frequently used request structures, by adhering to object-oriented principles.
Creating a `PostRequestPojo` class with `setName()`, `setLocation()`, `setPhone()`, and `setCourses()` methods, then instantiating it `PostRequestPojo data = new PostRequestPojo();` and calling `data.setName("Scott");`.
  • This approach involves storing the request payload in a separate `.json` file within the project.
  • Java's `java.io.File`, `FileReader`, and `org.json.JSONTokener` are used to read the file content.
  • The file content is parsed into a `JSONObject`.
  • Similar to the `org.json` method, the `JSONObject` must be converted to a String before sending.
Leveraging external JSON files is ideal when the request payload is pre-defined, large, or needs to be managed independently of the test code, facilitating data-driven testing.
Creating a `body.json` file with the payload and then reading it using `File file = new File("./body.json");`, `FileReader reader = new FileReader(file);`, and `JSONObject jsonObject = new JSONObject(new JSONTokener(reader));`.
  • After creating and sending a POST request, a DELETE request is often performed to clean up the created test data.
  • The DELETE request uses the ID of the resource created by the POST request.
  • Test execution involves running the Java methods using a test runner like TestNG.
  • Assertions are made on the response status code and potentially other fields to validate the request's success.
Implementing cleanup steps ensures that test environments remain clean and consistent, preventing data pollution and test interference between runs.
Executing a DELETE request to `/students/{id}` with a status code validation of 200 after a successful POST request.

Key takeaways

  1. 1POST requests require a body, which can be constructed in multiple ways to suit different data sources and complexities.
  2. 2HashMaps are suitable for simple, flat JSON structures directly within code.
  3. 3The org.json library offers a dedicated JSON object but requires string conversion for the request body.
  4. 4POJO classes provide an object-oriented approach, enhancing code structure and maintainability for request payloads.
  5. 5External JSON files are useful for managing pre-defined or large payloads independently of the test code.
  6. 6When using org.json or external files parsed into JSONObjects, converting the object to a String is a critical step before sending it as the request body.
  7. 7API testing often involves creating a resource via POST and then cleaning it up via DELETE to maintain test environment integrity.

Key terms

POST RequestRequest BodyRestAssuredHashMaporg.jsonJSONObjectPOJO (Plain Old Java Object)GetterSetterJSONTokenerJSON ServerAPI Testing

Test your understanding

  1. 1What are the four primary methods demonstrated for creating a POST request body in RestAssured?
  2. 2Why is it necessary to convert a `JSONObject` to a String when using the `org.json` library or reading from an external file?
  3. 3How does using a POJO class differ from using a HashMap for constructing a request body, and what are the advantages?
  4. 4When would you choose to use an external JSON file for your request body instead of creating it directly in your code?
  5. 5What is the purpose of performing a DELETE request after a POST request in API testing?

Turn any lecture into study material

Paste a YouTube URL, PDF, or article. Get flashcards, quizzes, summaries, and AI chat — in seconds.

No credit card required