Verify that the API returns a success message if the resource is created successfully.
Language: Java
Framework: Rest assured
1import org.junit.jupiter.api.Test;2import io.restassured.RestAssured;3import io.restassured.http.ContentType;45public class APITest {67 @Test8 public void createResourceTest() {910 // Assume the endpoint for creating resource is "https://example.com/api/create"11 // Assume that the resource is in JSON format12 // Assume that the payload for creating the resource is {"name": "John", "age": 30}1314 String resourceEndpoint = "https://example.com/api/create";15 String requestBody = "{\"name\": \"John\", \"age\": 30}";1617 RestAssured.given()18 .contentType(ContentType.JSON)19 .body(requestBody)20 .when()21 .post(resourceEndpoint)22 .then()23 .statusCode(200); // assume that the success status code is 2002425 // Uncomment the following code to connect to remote client with desired capabilities26 // DesiredCapabilities capabilities = new DesiredCapabilities();27 // capabilities.setBrowserName("chrome");28 // WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);29 // driver.quit();30 }31}
Language: Javascript
1// Mocha and Chai.23//Assumptions:4//1. The API endpoint is accessible.5//2. The API endpoint returns a JSON response with a 'success' field.6//3. A valid 'POST' request with a JSON payload creates a new resource.78const request = require('request');9const expect = require('chai').expect;1011describe('API Testing', () => {12 it('Check successful creation', (done) => {13 const options = { //assumes the API endpoint is localhost:3000/create14 url: 'http://localhost:3000/create',15 method: 'POST',16 json: {17 "name": "Test",18 "age": 20,19 "gender": "female"20 }21 };22 request(options, (err, res, body) => {23 expect(res.statusCode).to.equal(200); //assumes a status code of 200 for success24 expect(body.success).to.be.true; //assumes the success field is present and true25 done();26 });27 });28});2930//Assumes remote client with desired capabilities is accessible31// const webdriver = require('selenium-webdriver');32// const remote = require('selenium-webdriver/remote');33// const capabilities = webdriver.Capabilities.chrome();34// const chromeOptions = {35// 'args': ['--headless']36// };37// capabilities.set('chromeOptions', chromeOptions);38// const driver = new webdriver.Builder()39// .usingServer('http://localhost:4444/wd/hub')40// .withCapabilities(capabilities)41// .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.
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