Verify that the API returns an error message if the request payload is missing.
Language: Java
Framework: Rest assured
1//Assumptions - API endpoint is already available and request has to be sent using POST method23import org.testng.annotations.Test;4import io.restassured.http.ContentType;5import static io.restassured.RestAssured.*;67public class ApiTest {89 @Test10 public void testMissingPayload(){11 //Local Driver Initialization12 RestAssured.baseURI = "http://localhost:8080"; //Assuming API endpoint is running on localhost:808013 given().14 contentType(ContentType.JSON).15 when().16 post("/api/create").17 then().18 assertThat().19 statusCode(400); //Assuming 400 Bad Request is the expected status code when payload is missing20 21 //Remote Client Initialization with Desired Capabilities22 //DesiredCapabilities capabilities = new DesiredCapabilities();23 //capabilities.setCapability("platformName", "Android");24 //capabilities.setCapability("platformVersion", "11");25 //capabilities.setCapability("deviceName", "Pixel 4");26 //capabilities.setCapability("appPackage", "com.example.app");27 //capabilities.setCapability("appActivity", "MainActivity");28 //capabilities.setCapability("noReset", true);29 //driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); //Assuming Appium server is running on localhost:472330 }31}
Language: Javascript
1// Mocha/Chai.23//Assuming the API endpoint is http://yourapi.com/endpoint4//Assuming the payload parameter is 'data'5//Assuming the returned error message is 'Payload is missing'678const axios = require('axios');9const assert = require('chai').assert;1011describe('API Testing', () => {12 it('should return error message if the request payload is missing', async () => {13 14 //local driver initialization15 let response = await axios.post('http://yourapi.com/endpoint', {});16 assert.strictEqual(response.status, 400);17 assert.strictEqual(response.data, 'Payload is missing');18 19 //remote client initialization20 /*const webdriver = require('selenium-webdriver');21 const caps = {22 browserName: 'chrome',23 platform: 'WINDOWS',24 version: '10'25 };26 const driver = new webdriver.Builder()27 .usingServer('http://localhost:4444/wd/hub')28 .withCapabilities(caps)29 .build();3031 await driver.get('http://yourapi.com/endpoint');32 const payloadError = await driver.findElement(webdriver.By.id('payload_error')).getText();33 assert.include(payloadError, 'Payload is missing');34 driver.quit();*/35 36 });37});3839// Note: I have provided sample code for both local driver and remote client initialization in JavaScript using Mocha and Chai. You can uncomment the code for remote client initialization and modify the desired capabilities as per your requirement.
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing