API Testing : Check non-existent resource

Verify that the API returns an error message if the requested resource does not exist.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the base URI of the API2String baseURI = "http:/​/​localhost:8080/​api";3/​/​Assuming the endpoint for the non-existent resource4String nonExistentResource = "/​non-existent-resource";56/​/​Connecting to local driver7RestAssured.baseURI = baseURI;89/​/​Creating request specification using default contentType as JSON10RequestSpecification requestSpec = RestAssured.given();1112/​/​Creating response specification with 404 status code and error message13ResponseSpecification responseSpec = new ResponseSpecBuilder()14 .expectStatusCode(404)15 .expectBody("message",equalTo("Resource not found"))16 .build();1718/​/​Sending GET request to non-existent resource19given().spec(requestSpec).when().get(nonExistentResource).then().spec(responseSpec);2021/​/​Assuming desired capabilities for remote client22DesiredCapabilities capabilities = new DesiredCapabilities();23capabilities.setPlatform(Platform.WINDOWS);24capabilities.setBrowserName("chrome");25capabilities.setVersion("latest");2627/​/​Connecting to remote client28WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);29/​/​Implement rest of the code using Remote WebDriver

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions: 4/​/​1. API endpoint is known.5/​/​2. Non-existent resource request returns a 404 Status Code.6/​/​3. Error message for non-existent resource is "Resource not found".7/​/​4. Mocha and Chai are installed globally.89const request = require('request-promise');10const expect = require('chai').expect;1112describe('API Testing - Check Non-existent Resource', function() {1314 it('Should return an error message if the requested resource does not exist', async function() {15 try {16 const options = {17 method: 'GET',18 uri: '<API-Endpoint>/​non-existent-resource',19 json: true20 };21 22 const response = await request(options);23 24 /​/​Expecting response status code to be 404.25 expect(response.statusCode).to.equal(404);26 27 /​/​Expecting response body to contain error message for resource not found.28 expect(response.body.message).to.equal('Resource not found');29 30 } catch(error) {31 /​/​Throw an error if request fails.32 throw new Error(`Failed to check non-existent resource: ${error.message}`);33 }34 });35 36});3738/​/​Note:39/​/​To execute the test case, run the command 'mocha <file-name>.js' in command prompt/​terminal.

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