BreadcrumbHomeResourcesBlog How To Retrieve Database Data For API Testing With JMeter April 2, 2021 How to Retrieve Database Data for API Testing with JMeterOpen Source AutomationAPI TestingBy Sergey HorbanWhen performing API testing with JMeter, we always have to go to the database to check the values that the tested API returns. The data samples that need to be tested in the database can be either simple or complex, which leads to an increase in the time required to run the tests. It also often happens that the database has a limited number of connected users and running execution tests that can address the database. In such a case, we will get a negative result, because the database will not allow us to connect.If such cases occur during testing, a possible solution is to retrieve data from the database onto our local machine just once and use it for all the tests you need to perform for the API. This blog post will show you how to do that by using Apache JMeter™.Table of ContentsWhy API Testing With JMeter?How to Retrieve Database Data For API Testing With JMeterBottom LineTable of Contents1 - Why API Testing With JMeter?2 - How to Retrieve Database Data For API Testing With JMeter3 - Bottom LineBack to topWhy API Testing With JMeter?There are many benefits to doing API testing with JMeter. First, it is an open-source solution with many plugins to help with performance and load testing. Second, JMeter can be used with any platform that has a JVM, such as Windows, Linux, and macOS, since it is based on Java.In addition to API testing, JMeter is also very popular for performance testing, load testing, and stress testing.Suppose that we have an API that takes the parameter "city_id" and according to the value of this parameter, the API returns the parameter "cityName" = "city" from the table "city".Back to topHow to Retrieve Database Data For API Testing With JMeterTable "city"We will perform 5 API tests with JMeter with the following values of "city_id": 1,2,3,4,5. The data from the database that is used to verify that the API returned a correct answer, will be retrieved once, before the tests begin. This will reduce the number of requests in the database and the time to run the tests.To run this script, you need to do the following:1. Configure the connection to the database.2. In the "JDBC Request" add the following example code: select*fromcitywherecity_idin(1,2,3,4,5) This is an SQL query that selects from the table "city" all the rows for which city_id = 1,2,3,4,5. With this data, the result of each of our tests will be checked.dataFromDB is a variable that will reference the data retrieved from the database. Now we can proceed with the rest of our test.3. JDBC Request -> Add -> Assertions -> BeanShell Assertion4. In the "BeanShell Assertion" add the following example code if(!ResponseCode.equals("200")||vars.getObject("dataFromDB").size()==0){FailureMessage="!!!!!!!!!!! No connection to the database or data not received !!!!!!!!!!!";Failure=true;prev.setStopThread(true);} This code does the following: if the code in the response from the database is not equal to "200" or we get a response from the database that does not contain any rows, the test will be stopped and the following message will appear "!!!!!!!!!!! No connection to the database or data not received !!!!!!!!!!! "4. Preinstall the Dummy Sampler from the JMeter plugins manager and perform the following: ThreadGroup -> Add -> Sampler -> jp@gc - Dummy SamplerThe Dummy Sampler will simulate a response from the API. Add 5 items to these elements (5 API tests with JMeter).5. In the Dummy Sampler add the following data: jp@gc-DummySampler#1-{"cityName":"A Corua (La Corua)"}jp@gc-DummySampler#2-{"cityName":"Abha"}jp@gc-DummySampler#3-{"cityName":"Abu Dhabi"}jp@gc-DummySampler#4-{"cityName":"Acua"}jp@gc-DummySampler#5-{"cityName":"Adana"} 6. Dummy Sampler -> Add -> Post Processors -> Regular Expression ExtractorThe "Regular Expression Extractor" will be used to automatically get the value of the "cityName" parameter from the API response. This item must be added for each Dummy Sampler7. In the “Regular Expression Extractor”, add the following:cityNameFromApi is a variable that will store the value of the "cityName" parameter from the API response"cityName": (. +?)} is a regular expression that gets the value of the "cityName" parameter from the API response1 means that it is necessary to use the first value to be obtained by the regular expressionnull - if the value of the parameter "cityName" from the API response is not received, the variable cityNameFromApi will be set to "null"8. Dummy Sampler -> Add -> Assertions -> BeanShell Assertion. In the BeanShell Assertion, add the following example code: booleancomparisonResult=false;for(inti;i<vars.getObject("dataFromDB").size();i++){if(vars.getObject("dataFromDB").get(i).get("city").equals(${cityNameFromApi_g1})){comparisonResult=true;}}if(!comparisonResult){FailureMessage="!!!!!!!!!!! Test failed !!!!!!!!!!!";Failure=true;prev.setStopThread(true);} The BeanShell Assertion and code must be added for each Dummy Sampler.The code above does the following:boolean comparisonResult = false; - creates a variable of boolean data type, which will store the result of comparing the API response with the value from the database. The initial value is set to "false".vars.getObject ("dataFromDB"). size ():vars.getObject ("dataFromDB") returns all the data referenced by the variable "dataFromDB". This variable is used when receiving a response from the databasesize () - returns the number of rows that we received from the databasefor (int i; i is a loop that will be executed until the value of the number of iterations exceeds the number of rows received from the database$ {cityNameFromApi_g1} - a reference to the value of the parameter "cityName" from the API response that we get using the regular expressionif (vars.getObject ("dataFromDB"). get (i) .get ("city"). equals ($ {cityNameFromApi_g1})) {comparisonResult = true;} - at each iteration of the loop, it will be checked that the value of the parameter "cityName" from the API response is present in the data that we received from the database and to which we refer using the "dataFromDB" variable. If the condition is satisfied, then the comparisonResult variable will be assigned a logical value of "true"if (! comparisonResult) {FailureMessage = "!!!!!!!!!!! Test failed !!!!!!!!!!!";Failure = true;prev.setStopThread (true);} - After the for loop completes its work, if the value of the comparisonResult variable is not "true", then the message "!!!!!!!!!!! Test failed !!!!!!!!!!!" will be displayed. and the test will be stopped8. Thread Group -> Add -> Listener -> View Results TreeThe View Results Tree allows you to display each sent request and the results.The general view of the test scenario will have the form shown in the image below:Back to topBottom LineThat’s it! You now know how to do faster API testing with JMeter by retrieving data from the database to your local machine. To learn more about what you can do with JMeter testing, check out our free JMeter academy at BlazeMeter University.This blog was originally published on February 1, 2018, and has since been updated for accuracy and relevance.START TESTING NOWBack to top
Sergey Horban QA Specialist Sergey Horban is a QA Engineer with 3+ years of experience in automation and manual testing, bug-tracking and test management.