Let's start with some basics about API. An API – application
programming interface – allows your
product or service to talk to other products or services. In this way, an API
allows you to open up data and functionality to other developers and to other
businesses. As API is a bridge between you application with third parties so
it's functional, performance and security test is very important.
Required tool set:
1. Your favorite IDE (Sprig Tool Suite 3.8.2 in my case)
2. MySQL database engine
3. Latest version of JMeter
4. Postman (optional)
There are lots of public APIs are available but we will use
our own to have a better understanding how a RESTful API works. Clone or
download this project: https://github.com/muhin/RestWebService.git. It's a
sprint boot REST web service. Use your favorite IDE to import as maven project.
You can also use spring tool suite.
Now create a database in MySQL named country and then use the following scripts
to make this process faster:
CREATE
TABLE `country`.`country` (
`id` INT NOT NULL
AUTO_INCREMENT COMMENT '',
`country_name`
VARCHAR(255) NULL COMMENT '',
`population`
BIGINT(10) NULL COMMENT '',
PRIMARY KEY
(`id`) COMMENT '');
Now go to your IDE and expand src/main/resources. Open application.properties
file and change datasource url, username and password. If your HTTP port 8080
is in use then change the default port. Required configuration is done now
right click on your project and go to Run As -> Spring Boot App, if
everything is ok then you can see the application is running in console. Type
http://localhost:8080/ in browser and you can see hello world. Now our
application is ready for testing!
Our database is now blank, so we have to insert some data in
it. We will use JMeter to do this task. You can use postman as well. In the
later part I will show you how easily we can read CSV file to insert multiple
data using JMeter. Now insert a single record.
Open JMeter and add a thread group, Right click Test plan ->
Add -> Threads (users) -> Thread group, rename as your liking (REST user
in my case) but do not change the other settings. Because we are going to add a
single record. Now add a HTTP header manager, right click on your thread group ->
Add -> Config Element -> HTTP header manager. We are going to send data
as JSon format so add a row in the Name field type Content-Type and Value field
application/json.(image).
Now add HTTP request defaults, right click on thread group ->
Add -> Config Element -> HTTP request
defaults. Add your server details as like following image: (htttp request
defaults)
Add a HTTP request sampler to make a specific request to the
API. Right click on thread group -> Add -> Sampler -> HTTP request. You
can find different method in the controller file CountryController.java. We are
going to POST some data in body. (body data)
Finally add View result tree listener to see the result. Right
click on thread group -> Add -> Listener -> View result tree.
Now run your script, you can see result in view result tree
listener. (view result)
Comments
Post a Comment