Verify that the API correctly handles database transactions and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assuming the API Endpoint URL2String apiUrl = "https://api.example.com/transactions";34//Assuming the expected HTTP status code5int expectedStatusCode = 200;67//Assuming the expected error message8String expectedErrorMessage = "Database transaction handled correctly";910@Test11public void testDatabaseTransactionHandling() {1213 //Build request object14 RequestSpecification request = RestAssured.given();1516 //Add request headers17 request.header("Content-Type", "application/json");18 request.header("Authorization", "Bearer " + access_token);1920 //Assuming the request payload for adding transaction21 JSONObject requestParams = new JSONObject();22 requestParams.put("transaction_id", "12345");23 requestParams.put("amount", "100");24 requestParams.put("description", "Test transaction");25 request.body(requestParams.toJSONString());2627 //Send POST request to add transaction28 Response response = request.post(apiUrl);29 int statusCode = response.getStatusCode();30 String responseBody = response.getBody().asString();3132 //Assert response status code33 Assert.assertEquals(expectedStatusCode, statusCode);3435 //Assert response error message36 Assert.assertEquals(expectedErrorMessage, responseBody);37 38 //Assuming remote driver with desired capabilities39 //DesiredCapabilities capabilities = new DesiredCapabilities();40 //capabilities.setCapability("browserName", "Chrome");41 //capabilities.setCapability("version", "91.0");42 //capabilities.setCapability("platform", "Windows 10");43 //RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);44}
Language: Javascript
1// Mocha and Chai.23//Assumptions: 4//1. The API is built using Express framework and connects to a MongoDB database.5//2. The API returns a 200 HTTP status code on successful transaction and 500 HTTP status code on failure.6//3. Error message is returned in JSON format with error message and HTTP status code.78const mongoose = require('mongoose');9const chai = require('chai');10const chaiHttp = require('chai-http');11const expect = chai.expect;12const app = require('../index');1314chai.use(chaiHttp);1516describe('API Testing - Check database transaction handling', function() {1718 before((done) => {19 mongoose.connect('mongodb://localhost/testDB', { useNewUrlParser: true }, (err) => {20 if (err) {21 console.error(err);22 process.exit(1);23 }24 done();25 });26 });2728 after((done) => {29 mongoose.connection.close();30 done();31 });3233 it('should handle database transactions correctly and return 200 HTTP status code on success', function(done) {34 chai.request(app)35 .post('/api/addUser')36 .send({37 name: 'John Doe',38 email: 'johndoe@test.com'39 })40 .end((err, res) => {41 expect(err).to.be.null;42 expect(res).to.have.status(200);43 expect(res.body.message).to.equal('User added successfully');44 done();45 });46 });4748 it('should handle database transactions correctly and return 500 HTTP status code on failure', function(done) {49 chai.request(app)50 .post('/api/addUser')51 .send({52 name: 'John Doe',53 email: 'johndoe@test.com'54 })55 .end((err, res) => {56 expect(err).to.be.null;57 expect(res).to.have.status(500);58 expect(res.body.message).to.equal('Error adding user');59 done();60 });61 });6263 //Sample code to connect to remote client with desired capabilities64 /*65 const webdriver = require('selenium-webdriver');66 const capabilities = {67 'browserName': 'chrome',68 'browserVersion': 'latest',69 'platformName': 'Windows 10'70 };71 const driver = new webdriver.Builder()72 .usingServer('http://localhost:4444/wd/hub')73 .withCapabilities(capabilities)74 .build();75 */7677});7879//Assumptions:80//1. The API returns error message in JSON format81//2. The API returns status code 200 on successful transaction and 500 on failure82//3. The API is built on Express framework and is using MySQL database8384const request = require('supertest');85const app = require('../index');8687describe('API Testing - Check database transaction handling', function() {8889 it('Should handle database transactions correctly and return 200 status code on success', function(done) {90 request(app)91 .post('/user')92 .send({93 name: 'John Doe',94 age: 20,95 email: 'johndoe@abc.com'96 })97 .expect(200)98 .end(function(err, res) {99 if (err) return done(err);100 expect(res.body.message).to.equal('User has been successfully created');101 done();102 });103 });104105 it('Should handle database transactions correctly and return 500 status code on failure', function(done) {106 request(app)107 .post('/user')108 .send({109 name: 'John Doe',110 age: 20111 })112 .expect(500)113 .end(function(err, res) {114 if (err) return done(err);115 expect(res.body.message).to.equal('Error in creating user');116 done();117 });118 });119120 //Sample code to connect to remote client with desired capabilities121 /*122 const webdriver = require('selenium-webdriver');123 const capabilities = {124 'browserName': 'chrome',125 'browserVersion': 'latest',126 'platformName': 'Windows 10'127 };128 const driver = new webdriver.Builder()129 .usingServer('http://localhost:4444/wd/hub')130 .withCapabilities(capabilities)131 .build();132 */133134});
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