API Testing : Check error handling

Verify that the API correctly handles error conditions and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API end-point and required headers are already defined2import org.junit.Test;3import static org.hamcrest.Matchers.equalTo;4import io.restassured.response.Response;5import io.restassured.RestAssured;67public class APITesting {8 @Test9 public void testErrorHandling() {10 /​/​Assuming a valid request11 Response response = RestAssured.given().header("Content-Type", "application/​json")12 .body("{\"username\":\"TestUser\",\"password\":\"Password123\"}").when().post("/​login");1314 /​/​Asserting the HTTP status code15 response.then().statusCode(401);1617 /​/​Asserting the error message18 response.then().body("errorMessage", equalTo("Invalid credentials"));1920 /​/​Connecting to remote client with desired capabilities21 /​/​Assuming the following capabilities22 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();23 /​/​capabilities.setCapability("browserName", "Chrome");24 /​/​capabilities.setCapability("platformName", "Android");25 /​/​capabilities.setCapability("deviceName", "Pixel 2 XL");26 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);27 }28}

Language: Javascript

copy
1/​/​ Mocha and Chai23const chai = require('chai');4const chaiHttp = require('chai-http');5const expect = chai.expect;6chai.use(chaiHttp);78describe('API Testing - Error Handling', function() {9 it('Should return the correct HTTP status code and error message on error', function(done) {10 chai.request('http:/​/​localhost:3000')11 .get('/​api/​example')12 .end(function(err, res) {13 expect(res).to.have.status(404);14 expect(res).to.be.json;15 expect(res.body).to.have.property('error').that.equals('Resource not found');16 done();17 });18 });19});2021/​/​ To connect to remote client with desired capabilities, we can use the following code:22/​/​ const webdriver = require('selenium-webdriver');23/​/​ const remote = require('selenium-webdriver/​remote');24/​/​ const capabilities = {25/​/​ 'browserName': 'chrome',26/​/​ 'browserVersion': '91.0',27/​/​ 'platformName': 'Windows 10',28/​/​ 'seleniumVersion': '3.141.59'29/​/​ };30/​/​ const driver = new webdriver.Builder()31/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')32/​/​ .withCapabilities(capabilities)33/​/​ .build();

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