Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Retrieve Database Data for API Testing With JMeter

DZone's Guide to

How to Retrieve Database Data for API Testing With JMeter

In this quick tutorial, learn how to run your API tests quicker by retrieving data from the database to your local machine.

· Database Zone ·
Free Resource

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

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"

We will perform five 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.

Configure the connection to the database, as described here "MySQL Database and JMeter - How to Test Your Connection. In JDBC Request, add the following example code:

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. Do DBC Request -> Add -> Assertions -> BeanShell Assertion.

In BeanShell Assertion, add the following example code:

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

Preinstall the Dummy Sampler from the JMeter plugins manager and perform the following. ThreadGroup -> Add -> Sampler -> jp@gc - Dummy Sampler.

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

In the Dummy Sampler, add the following data:

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

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.

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

Do Dummy Sampler -> Add -> Assertions -> BeanShell Assertion. In the BeanShell Assertion, add the following example code:

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

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

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:

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.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Topics:
database ,tutorial ,api testing ,jmeter ,data retrieval

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}