Verify that the API returns an error message if the request payload contains invalid data.
Language: Java
Framework: Rest assured
1import org.junit.jupiter.api.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class InvalidPayloadTest {67 @Test8 public void testInvalidPayload() {9 //Assuming the API endpoint is https://example.com/api/users10 //Assuming the request method is POST11 //Assuming the request payload should contain a user's name, email and password in JSON format like:12 /*13 {14 "name": "John Doe",15 "email": "johndoe@example.com",16 "password": "123456"17 }18 */19 20 //Invalid payload - missing email field21 String invalidPayload = "{\n" +22 " \"name\": \"John Doe\",\n" +23 " \"password\": \"123456\"\n" +24 " }";2526 given()27 .contentType("application/json")28 .body(invalidPayload)29 .when()30 .post("https://example.com/api/users")31 .then()32 .statusCode(400) //Assuming 400 is the correct status code for invalid payloads33 .body("message", equalTo("Invalid request payload")); //Assuming "Invalid request payload" is the error message returned for invalid payloads3435 //Connecting to remote client with desired capabilities (assuming remote address is 192.168.1.100:4444/wd/hub)36 /*37 DesiredCapabilities caps = new DesiredCapabilities();38 caps.setBrowserName("chrome");39 caps.setPlatform(Platform.WINDOWS);4041 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"), caps);42 driver.get("https://example.com");43 */44 }45}
Language: Javascript
1// Mocha and Chai.23const request = require('request');4const expect = require('chai').expect;56describe('API Testing', function() {7 it('should return an error message for invalid payload', function(done) {8 9 //Assuming the endpoint for this API testing is http://example.com/api10 const options = {11 url: 'http://example.com/api',12 method: 'POST',13 json: {14 //Assuming an invalid payload15 data: 'invalid_data'16 }17 };18 19 //Local driver20 request(options, function(error, response, body) {21 expect(body).to.have.property('error');22 expect(body.error).to.equal('Invalid Payload');23 done();24 });25 26 //Remote client with desired capabilities27 // const capabilities = {28 // browserName: 'chrome'29 // };30 // const driver = new webdriver.Builder()31 // .usingServer('http://remotehost:4444/wd/hub')32 // .withCapabilities(capabilities)33 // .build();34 // driver.get('http://example.com/api');35 // driver.executeScript('...');36 });37});
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