Sergey Horban is a QA Engineer with 3+ years of experience in automation and manual testing, bug-tracking and test management.

Learn JMeter in 5 Hours

Start Learning
Slack

Run massively scalable performance tests on web, mobile, and APIs

Feb 01 2018

How to Retrieve Database Data for API Testing with JMeter

When performing API testing, 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 on to 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™.

 

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".

 

Table "city"

 

retrieve database data for api testing

 

We will perform 5 tests for our API 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, as described here "MySQL Database and JMeter - How to Test Your Connection".

 

2. In the "JDBC Request" add the following example code:

 

select * 
from city
where city_id in (1,2,3,4,5)

 

gat database data for api testing

 

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 Assertion

 

api testing from databases

 

4. 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);
	
}

 

api testing databases

 

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 Sampler

 

api testing databases with jmeter

 

The Dummy Sampler will simulate a response from the API. Add 5 items to these elements (5 tests).

 

5. In the Dummy Sampler add the following data:

 

jp@gc - Dummy Sampler #1 - {"cityName": "A Corua (La Corua)"}
jp@gc - Dummy Sampler #2 - {"cityName": "Abha"}
jp@gc - Dummy Sampler #3 - {"cityName": "Abu Dhabi"}
jp@gc - Dummy Sampler #4 - {"cityName": "Acua"}
jp@gc - Dummy Sampler #5 - {"cityName": "Adana"}

 

getting data from databases to local machine for api testing

 

6. Dummy Sampler -> Add -> Post Processors -> Regular Expression Extractor

 

api testing data with jmeter

 

The "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 Sampler

 

7. In the “Regular Expression Extractor”, add the following:

 

guide to getting database data to machine

 

  • 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 response
  • 1 means that it is necessary to use the first value to be obtained by the regular expression
  • null - 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:

 

boolean comparisonResult = false;

for (int i; 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);
}

 

jmeter api testing of databases

 

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 database
  • size () - returns the number of rows that we received from the database

 

for (int i; i <vars.getObject ("dataFromDB"). size (); 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 expression

 

if (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 stopped

 

8. Thread Group -> Add -> Listener -> View Results Tree

 

get database data to machine with jmeter learn how

 

The 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:

 

api testing with jmeter learn how quickly

 

That’s it! You now know how to run your API tests quicker by retrieving data from the database to your local machine. To learn more about what you can do with JMeter, check out our free JMeter academy.

 

Click here to subscribe to our newsletter.

 

To try out API testing with BlazeMeter, just put your URL and response in the box below and your test will start in minutes. You can also request a demo.

Interested in writing for our Blog?Send us a pitch!

Your email is required to complete the test. If you proceed, your test will be aborted.