- 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
- 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.
- Language - Java
- IDE - VS code
- Dependency Management Tool - Maven
- Framework - TestNG
- io.rest-assured
- org.testng
- com.googlecode.json-simple
- 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!!
- Check if you have npm and node.js package installed in cmd and if not then install it
npm -v-> Check the npm versionnode -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
-
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
-
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 |
- 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
- https://api.example.com/users?id=10
- https: Scheme
- api.example.com: Host
- /users: Resource Path
- id=10: Query String