DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Leveraging Query Parameters for Efficient Data Filtering in REST APIs
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • 2-Tier Architecture vs 3-Tier Architecture in DBMS
  • Building REST API Backend Easily With Ballerina Language

Trending

  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Retrieve Database Data for API Testing With JMeter

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.

By 
Sergey Horban user avatar
Sergey Horban
·
Feb. 05, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
18.9K Views

Join the DZone community and get the full member experience.

Join For Free

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.

API Database Data (computing) API testing

Published at DZone with permission of Sergey Horban, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Leveraging Query Parameters for Efficient Data Filtering in REST APIs
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • 2-Tier Architecture vs 3-Tier Architecture in DBMS
  • Building REST API Backend Easily With Ballerina Language

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!