Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atiarmridul/java-assignment
A simple task containing HTTP method testing, API error handling & content validation.
https://github.com/atiarmridul/java-assignment
Last synced: 16 days ago
JSON representation
A simple task containing HTTP method testing, API error handling & content validation.
- Host: GitHub
- URL: https://github.com/atiarmridul/java-assignment
- Owner: atiarmridul
- Created: 2024-03-24T21:44:00.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-09-08T06:52:00.000Z (4 months ago)
- Last Synced: 2024-09-08T07:48:38.749Z (4 months ago)
- Language: Java
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JAVA Practice - REST Assured
A simple task containing HTTP method testing, API error handling & content validation.
## Technology Used
**Programming Language:** JAVA
**Build Tool:** Maven
**IDE:** IntelliJ IDEA
## Prerequisite
Must have the following dependencies in the Maven build file.
- TestNG
- Rest Assured## Project Summary
I have executed 4 simple tests, they are following:
- **testGetRequestSuccess:**
Sends a GET request to retrieve a specific resource and asserts that the response status code is 200 and that the response body contains a specific string.```bash {"id":"01J785Q34DDJ74TAK0W3P25YW7"}
public void testGetRequest() {
// Define base URI
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";// Send GET request
Response response = RestAssured.get("/posts/1");// Verify status code
Assert.assertEquals(response.getStatusCode(), 200);// Verify response body
Assert.assertTrue(response.getBody().asString().contains("userId"));}
```
- **testGetRequestNotFound:**
Sends a GET request to retrieve a resource that does not exist and asserts that the response status code is 404 (Not Found).```bash {"id":"01J785Q34DDJ74TAK0W61BNWC6"}
public void testGetRequestNotFound() {// Define base URI
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";// Send a GET request to a non-existent resource
Response response = RestAssured.get("/posts/1000");// Verify status code for resource not found
Assert.assertEquals(response.getStatusCode(), 404);
}```
- **testPostRequest:**
Sends a POST request to create a new resource and asserts that the response status code is 201 (Created).```bash {"id":"01J785Q34DDJ74TAK0W6484P0N"}
public void testPostRequest() {
// Define base URI
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";// Send POST request
Response response = given()
.header("Content-type", "application/json")
.and()
.body("{ \"title\": \"Md.Atiar\", \"body\": \"bar\", \"userId\": 1 }")
.when()
.post("/posts");// Verify status code
Assert.assertEquals(response.getStatusCode(), 201);}
```
- **testPutRequest:**
Sends a PUT request to update an existing resource and asserts that the response status code is 200 (OK).```bash {"id":"01J785Q34DDJ74TAK0W79XSPE8"}
public void testPutRequest() {
// Define base URI
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";// Send PUT request
Response response = given()
.header("Content-type", "application/json")
.and()
.body("{ \"id\": 1, \"title\": \"Mridul\", \"body\": \"bar\", \"userId\": 1 }")
.when()
.put("/posts/1");// Verify status code
Assert.assertEquals(response.getStatusCode(), 200);
}```