Skip to content

Rahul1181/API_Testing_with_Rest_Assured

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 

Repository files navigation

API Testing with Rest Assured

  1. What is REST?
  • Rest or Representational State Transfer is a Java Library or Java API for testing Restful Web services.
  • Rest is used to run or invoke REST web services and validate response
  • Used to test XML or JSON based web services.
  • Supports HTTPS method like GET, POST, PUT, DELETE, OPTIONS, PATCH AND HEAD
  • Can integrate with TestNG or Junit.
  • Rest is implemented in Groovy
  1. What is Groovy?
  • Groovy is a JVM-based dynamic language that is fully compatible with Java. It is used in Jenkins pipelines, Gradle build scripts, and frameworks like Spock.

Prerequisites

  • Language - Java
  • IDE - VS code
  • Dependency Management Tool - Maven
  • Framework - TestNG

Helpful Tools

Maven Dependencies

  • io.rest-assured
  • org.testng
  • com.googlecode.json-simple

Website for Testing

How to set API key in REQRES in Windows?

  • Create an account in Reqres
  • Go to API Key
  • Create New API key and give this a name
  • Copy the API key and save it in local
  • Open CMD and paste the following command
    • setx REQ_RES_API_KEY="**YOUR_API_KEY**"
  • Close CMD and rerun
  • Use command echo %REQ_RES_API_KEY% and check if you get your API key
  • Now create a "EnvironmentConfig.java" file and add the details of API Key in it
  • And all set!!

Local Server Setup

  • Check if you have npm and node.js package installed in cmd and if not then install it
    • npm -v -> Check the npm version
    • node -v-> Check the node.js version
  • Run cmd as Administrator and paste the cmd : npm install -g json-server@0.17.4 -> Install the JSON Server in local
  • Then paste " json-server --watch db.json -> Starts the JSON server
  • You will have db.json created in the directory where you ran the cmd command
  • Copy and paste the local host URL: http://localhost:3000/
  • Now run and enjoy

Setup JSON Schema Validation

  1. Why do we need to JSON Schema Validation? Ans: JSON Schema testing ensures that the API response structure (contract) is correct and stable, independent of the actual data values. It checks:

    • Missing fields
    • Renamed fields
    • Wrong data types
    • Unexpected structure changes This is critical when:
    • Data is dynamic
    • IDs and timestamps change every run
  2. What JSON Schema Validation Checks ?

Check Example
Field existence id must exist
Data type id must be integer
Required fields name, email
Array vs object users must be array
Nested structure address.city
Enum values status ∈ [active, inactive]
Additional fields Optional / forbidden
  1. Setup:
  • Goto : JSON to JSON Schema Converter
  • Goto: Reqres and copy one of the JSON response
  • Paste the response in JSON schema generator
  • Goto target/classes and paste the code in schemaUserData.json (This is adding JSON FILE IN CLASSPATH)
  • Get the JSON schema Validator Repo and paste in POM file
  • Run the code JsonSchemaValidator.java

Points to Remember

About

Master API Testing with Rest Assured Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages