API Testing : Check resource not found

Verify that the API returns the correct HTTP status code for resource not found (e.g. HTTP 404 Not Found).

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is http:/​/​example.com/​api23import static io.restassured.RestAssured.*;4import static org.hamcrest.Matchers.*;56public class ApiTest {78 @Test9 public void testResourceNotFound() {1011 given()12 .when()13 .get("http:/​/​example.com/​api/​non_existing_resource")14 .then()15 .statusCode(404);1617 /​/​Code below to connect to remote client with desired capabilities18 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();19 /​/​capabilities.setCapability("platform", "Windows 10");20 /​/​capabilities.setCapability("browserName", "Chrome");21 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​127.0.0.1:4444/​wd/​hub"), capabilities);22 }23}

Language: Javascript

copy
1/​/​ Mocha23/​/​Assumptions: 4/​/​The API endpoint has been identified for this test case.5/​/​The GET method is used to retrieve resources from the endpoint.67const axios = require('axios');8const assert = require('assert');910describe('API Resource Not Found Test', function() {11 12 it('should return a 404 response', async function() {13 14 /​/​Assuming the resource at this endpoint doesn't exist and should return a 404 error15 const endpoint = 'https:/​/​example-api.com/​resources/​nonexistent'; 16 17 try {18 19 /​/​ Send GET request to endpoint20 await axios.get(endpoint);21 }22 catch(error) {23 24 /​/​ Verify that the correct HTTP status code is returned25 assert.equal(error.response.status, 404, 'Incorrect HTTP status code returned');26 }27 });28 29 /​/​Code to connect to remote client with desired capabilities30 /​/​const { Builder, Capabilities } = require("selenium-webdriver");31 /​/​const capabilities = Capabilities.chrome();32 /​/​capabilities.set("version", "91.0");33 /​/​capabilities.set("platform", "Windows 10");34 /​/​const driver = await new Builder()35 /​/​ .usingServer("http:/​/​localhost:4444/​wd/​hub")36 /​/​ .withCapabilities(capabilities)37 /​/​ .build();38 39});

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