๐Ÿ”ฌHTTP Requests with Java

GET Request

Test Get all Users

@Test
public void testGetAllUsers(){
  
  given()
    .when()
      .get("/users")  
    .then()      
      .statusCode(200)
}

POST Request

Test Create User

@Test
public void testCreateUser(){
  
  User user = new User("John", "Doe", 30);
  
  given()
    .body(user)
   .when()     
     .post("/users")
   .then()
     .statusCode(201)
}

PUT Request

Test Update User

@Test
public void testUpdateUser(){
  
  User updatedUser = new User("John", "Smith", 31);
  
  given()
    .pathParam("id", 1)
    .body(updatedUser)
   .when()     
     .put("/users/{id}") 
   .then()
     .statusCode(200)  
}

PATCH Request

Test Update User Name

@Test        
public void testUpdateUserName(){

  given()
    .pathParam("id", 1)
    .body("John Doe")  
  .when()        
    .patch("/users/{id}/name")
  .then()
    .statusCode(200)         
}

DELETE Request

Test Delete User

@Test
public void testDeleteUser(){

  given() 
    .pathParam("id", 1)   
  .when()      
    .delete("/users/{id}")
  .then()
    .statusCode(200)
}

Last updated