REST Assured: CRUD Framework for API Testing
Design Pattern for API Testing using Java Rest Assured Library.
Join the DZone community and get the full member experience.Join For Free
REST Assured Java Library - API Testing
REST Assured is an open-source (free) Java library available for testing primarily the RESTful web services. REST Assured is a Java domain-specific language (DSL) for simplifying testing of REST-based web services built on top of HTTP. It supports most commonly used HTTP verbs like GET, POST, PUT/PATCH DELETE, OPTIONS, HEAD which can be used to validate & verify the response of these requests.
Web API Communication
Designing Test Cases
Implementation of Test Cases: CRUD Pattern
Step 1: Create a Maven Java project in Eclipse with all the required dependencies in pom.xml as shown below:
Dependency Details io.rest-assured --> Rest Assured for testing API interactions(request & response),extract json/xml path. org.testng --> Designing the Test Frmework using TestNG Classes. com.google.code.gson --> Represent the request body data in the from of java objects like Map <--> JSONformat. com.github.javafaker --> To supply fake data for testing while sending request. com.jayway.jsonpath --> Extract data specifically from JSON file using jayway jsonpath.
Step 2: Design the Routes.java class to register all the available services with end-points as shown below:
Step 3: Design the UserEndpoints.java class to perform Create, Read, Update, Delete requests to the services using the above-created end-points as shown below:
Step 4: Design the User.java class to represents the data structure of the user payload which is in JSON format as shown below:
Step 5: Design the TestEndpoints.java class to perform the API Testing for all the services using the TestNG class as shown below:
Maven Java Project Structure:
***Note: Run the TestEndpoints.java class as TestNG test to observe the below output.
*** Note: Each time you run the code, a different username is generated by the Java Faker class.
Hence the above demo represents the completion of design using CRUD pattern, wherein we can modularize and separate the data layer, API calls, test codes. These test cases are completely designed independently of the testing framework. This framework ensures to have code reusability, modularity, and better maintenance of the codes.
Opinions expressed by DZone contributors are their own.