API Testing : Check successful deletion

Verify that the API returns a success message if the resource is deleted successfully.

Language: Java

Framework: Rest assured

copy
1/​/​ Assumptions: 2/​/​ 1. The API endpoint URL is stored in a variable called 'apiUrl'. 3/​/​ 2. The 'resourceId' of the resource to be deleted is stored in a variable called 'id'. 45import static io.restassured.RestAssured.*;6import org.testng.Assert;7import io.restassured.response.Response;89public class APITest {10 public void testDeleteResource() {11 /​/​ Delete the resource with the given ID12 Response response = given().delete(apiUrl + "/​resources/​" + id);1314 /​/​ Verify that the response status code is 200 OK15 Assert.assertEquals(response.getStatusCode(), 200);1617 /​/​ Verify that the response message contains the word "success"18 Assert.assertTrue(response.getBody().asString().contains("success"));19 }20 21 /​/​ Code to connect to remote client with desired capabilities22 /​/​ DesiredCapabilities capabilities = DesiredCapabilities.chrome();23 /​/​ WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);24}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. API endpoint for resource deletion: /​api/​deleteResource5/​/​2. Resource exists in the system before deletion.6/​/​3. Deletion is successful if response code is 200 and response body contains success message.78const chai = require('chai');9const chaiHttp = require('chai-http');10const expect = chai.expect;1112chai.use(chaiHttp);1314describe('API Deletion Test', () => {15 16 /​/​Local driver17 let server;1819 before(async () => {2021 /​/​Local driver22 server = require('../​app');23 await server.listen(3000);2425 });2627 after(async () => {2829 /​/​Local driver30 await server.close();3132 /​/​Connection to a remote client with desired capabilities33 /​/​await driver.quit();34 35 });3637 it('should return success message for successful resource deletion', async () => {3839 /​/​Assuming valid deletion payload40 const deletionPayload = {id: 'RESOURCE_ID'};4142 /​/​Assuming valid API endpoint for resource deletion43 const deletionEndpoint = '/​api/​deleteResource';4445 /​/​Making API call to delete resource46 const response = await chai47 .request(server)48 .delete(deletionEndpoint)49 .send(deletionPayload);5051 /​/​Asserting response status code is 20052 expect(response.status).to.equal(200);5354 /​/​Asserting response body contains success message55 expect(response.body.message).to.equal('Resource successfully deleted.');5657 });5859});

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing