Detailed Notes
Our journey into Restful API Testing Using RestAssured begins by looking at the different ways in which API testing can be done. Next, we look at how to use tools such as the browser developer tool, Swagger, and Postman to test APIs from a browser. Additionally, we look at how to use libraries like RestAssued and Karate to build continuous integration tests for our code projects. Finally, we wrap up our discussion by showing you how to write RestAssured tests against a real-world scenario.
Restful API Testing Using RestAssured Overview:
Overview of Restful API Different Types of HTTP Requests How to Manually Test a Restful API Use Postman and Swagger Write RestAssured Unit Test What is Restful API
REST APIs provide a flexible, lightweight way to integrate applications, and have emerged as the most common method for connecting components in microservices architectures.
-IBM
HTTP Requests
Get - Requests a representation of the specified resource. Requests using GET should only retrieve data. Post -Submits an entity to the specified resource, often causing a change in state or side effects on the server. Put - Replaces all current representations of the target resource with the request payload. Delete - Deletes the specified resource. - Mozilla
RestAssured
REST Assured is a Java DSL for simplifying testing of REST-based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and can be used to validate and verify the response of these requests.
- RestAssured
Start with a real-world example
To explain the principles of API testing we decided to begin with a real-world exercise and build out our test cases to support the following scenario. For instance, a company that wants to set up a Tutorial center at a university to help students study. Initially, they build a WS application that records the subjects that will be covered.
The current interface supports:
Create A Tutorial Retrieve All Tutorials Retrieve A Tutorial By Id Update A Tutorials By Id Find All Published Tutorials Find By Title Containing String Delete A Tutorial Delete All Tutorials
Restful API Testing Using RestAssured
Additional Resources
RestAssured Karate Postman Swagger Finally, this series comes from our mentoring/mastermind classes. These classes are virtual meetings that focus on how to improve our technical skills and build our businesses.  After all the goals of each member vary.  However, this diversity makes for great discussions and a ton of educational value every time we meet.  We hope you enjoy viewing this series as much as we enjoy creating it. As always, this may not be all new to you, but we hope it helps you be a better developer.
Transcript Text
[Music] welcome today we are going to be looking at restful api testing we're going to be looking at using tools such as rest assured karate we'll be discussing things like swagger postman and generally just talking about web service in general and how we go about testing them so the overview of this particular course this is going to be a three-part presentation we're going to focus on restful apis wrist assured karate the api testing tool not the actual physical act and we're going to look at a simple project using both so we'll kind of do a side-by-side comparison between the different libraries that are out there that we're going to test with so let's start by talking about what is restful api and before i go over this let's first go over that api is an application programming interface that is a set of rules that defines how the application or device can connect to and communicate with each other so when we talk about rest apis we're talking about providing a flexible lightweight way to integrate our applications and have emerge as the most common method for connecting components in a microservice architecture now this definition comes from ibm but typically if you search rest apis or restful api you come up with some type of definition similar to this and it's essentially a way to communicate between different applications across the web so it's just a kind of a template way for us to integrate our applications be it mobile be it web backend databases to web servers in our presentation today we're primarily going to focus on these four http methods now there are others and we'll look at mozilla briefly to go over the full list but the ones we're going to focus on are get post put and delete get is probably the most common http method and it is used to request a representation of a specified resource so this is kind of like where we get data or fetch data from a web service it could be a list it could be a record post is typically used for submitting an entity to be a specified resource this is where we actually have kind of like web forms where we push a bunch of information up to the web servers to the application we can also use put where we actually replace the current representations of the target resource with a different payload and finally we can use delete to remove a resource from the remote machine so today we're going to focus primarily on rest assured and looking at our test application that we're going to integrate with so rest assured is a java dsl and dsl stands for domain specific language and it's used for simplifying tests of a rest based service built on top of an http builder now rest assured supports post get put delete option patch and head now there are other types of requests that we can use but these are typically the ones that are generally used to validate and verify the response of the requests next we have karate and karate is another domain specific language and karate is an open source tool to combine api test automation mocks performance testing and ui automation into a single unified framework one of the things about karate is that it is built on the bdd syntax of cucumber so it is a gherkins like framework and what's nice about it is it well it does have a java hook where you can actually use java to expand upon your karate test it doesn't necessarily need java it can use a javascript and become a kind of an open standard for even non-programming and we'll look at that a little bit throughout this course finally at the end of this series we will talk about how karate and recitard kind of compare with one another how they match up how they stack up so throughout this entire presentation we're going to primarily be doing a demo and this demo is going to be comprised of an application so here is a simple overview of what it is we're going to be looking at so we have a company that wants to set up a tutorial center on a university to help students study they have started building a web service application that records the subjects that will be covered currently the interface supports things like post get delete and put and we're going to define some tests that test that implementation so we'll have things like creating a tutorial retrieving all the tutorials in the system retrieve a tutorial by id update a tutorial by ib find all published tutorials find a title containing a string we want to delete a specific tutorial and then we want to delete all the tutorials we'll walk through how to do it through postman we'll use swagger we'll look at using rest assured and we'll also look at doing this with karate so let's get started so to start with let's go out to the mozilla site that i referenced in the notes so here we see all the different types of http request methods that are available to us from the web so we have things like get head post put delete connect options trace and patch and a brief definition of each next i provided and slacked the link to rest assured so rest assured lives out on github.com forward slash rest dash assured forward slash rest dash assured now here on the main page you have a readme which gives you the basic overviews of what you can do with the language and we'll go over this a little more detail later i also provide a link to the query labs slash karate on github and a link from the karate that kind of gives a comparison between the two now the karate and the comparison will cover over the next two lessons but i just want to provide them now if you want to kind of read ahead before we get to those all right so the first thing we want to do is we have to have some type of web service application so as we talked about in our slides we have a test application already built so i have this demo application here that is a spring boot application with some additional components at it so let's look at the palm file so it's also a maven project so i typically use maven for all of my web applications because it's so easy to integrate with third-party libraries so here in our pom file we have a list of some dependencies now because i'm using spring sts and i'm using the spring framework i can just outright import the spring libraries that are already provided within the spring tool suite so that gives us access to our spring boot starter jpa i'm doing in kind of web driven database here so we're going to load things into memory we're using h2 database and i'm using spring boot starter web and by doing this i have access to the library so we can use spring boot i also included a parent to the spring boot library artifact which allows me to import and set the spring boot starter parent to 2.6.3 by doing this i do not have to specify the versions down here it will automatically pull in the dependencies from this library that i want to use all right in order to use rest assured we have to include these libraries here so we start with the basics i o rest assured and we need the rest assured artifact if we want to use json we need to include the json path artifact if you want xml you want to use the xml path and finally if you want to do some json scheme validations you need to include the json scheme schema validator so these four dependencies pretty much wrap up everything that you need for rest assured now if we want to include in our project wagger we also need to include spring docks prior to using spring docks you would have to include the spring fox swagger components but spring has now simplified this into spring docks so for swagger we need to use spring docks all right so that's our palm file so these are our dependencies so now if we go look at the project itself we have our source main java our source test java we also have a source main resources folder now because this is a spring boot application we need to put some of our configuration settings in the application properties since i have a database i need to configure my h2 so that i can enable the h2 in memory database and i give it a console path so i can access this if i want to access it from a database tool i also need to set my data source my class name user and password since we're also going to use jpa to tie into our database with our entities so we're going to use jpa show jpa properties hibernate dialect and hibernate dll auto by doing this this will show our sql in the console down here when we actually run or start up spring boot and finally to configure spider all we had to do is add these two property lines we had to set the spring.swagger ui path which is going to be api docs and we had to do springdocs.swagger operation sorter so how are we going to sort our methods and because i did this it's going to sort them alphabetically the project itself is fairly straightforward we have our demo application so this is our spring application so if you're doing spring boot you just have to have a spring boot class with main that essentially starts the spring application and that will be what's down here our little spring boot it essentially starts a tomcat web service for us our configuration file here i'm pre-loading some data within our database so i just have a command line being that will add two tutorial records to our database in memory every time we restart i find this is useful for testing because it gives us a starting point our web service itself is composed of a controller and we have these methods here we have get all tutorials get tutorials by id create tutorials update delete delete all and find buy published ids just have some basic logic in here the interesting one is the get we use our annotations for our different http requests so we have get we have post we have put and delete now this looks ugly and from a tester's perspective we really don't need to get into this level but i'm just kind of walking through how the project's set up then we have our model which contains tutorial and this is just our entity beam so we define it as entity we set our table name to tutorials in memory and we give it an id our title our description and our boolean for published and this is just a regular pojo so we have all of our getters and setters and then finally we set up the repository and we define the repository to give it some additional lookups we have that find by published which returns a list and we have find by titles containing which also returns a list so we can look up by the published boolean or we could look up by title otherwise we use the straight up getters and setters within our lookups so what's interesting here so if we do extends jpa this gives us that full kind of crud functionality that we'll see in our api with the addition of these two lookups so we talked about resources so now let's go down and let's look at what we have in our test so for this particular example since rest assured is a java library for testing apis i thought it'd be easier to just drop it right into our spring boot project here because it also gives me the ability to reuse our tutorial application for our tutorial sorry our tutorial entity beam so here we have just a typical spring boot test which is essentially j unit but it's going to kick off a spring boot instance to run rj unit test so we can actually deploy this test suite as a separate microservice and run it on a different system so you can have your api running on one and then kick off your test on another we also want to add test method order this way we can actually tell it the order that we want to run our test in and then we give it the name now in our slides we basically stated that we want to test these eight different components we want to create a tutorial test retrieve all tutorials retrieve a tutorial by id update find and delete so the first thing we need to do is we need to create a test so let's come in here so in java if you're using junit or test nd or most test frameworks you have a test annotation so we start with test now because i want to run these in a specific order i also can include the order annotation and for that it requires a number essentially a sequential number so we'll do one now since we want our first test to be create a tutorial we'll come down in here and we'll do void create a tutorial and let's make it camel case to comply with java naming conventions so right now we have now added an empty test to create a tutorial lookup now before we actually continue writing this out let's take a moment and actually start the api look at the console and then look at the api structure from swagger so let me restart spring boot spring starts up tomcat we initialize our h2 console for h2ui for our database lookup we run our configuration so he we build our table we be we build the create sequence hibernate and then we call and we insert our tutorials so we've added we've pre-loaded the tutorials with calculus and how to introduce testing into the development cycle we have our unique ids and we have our published boolean flag so the calculus is published my book is not so we have our description and we're not published so now that it's started if we actually go out and look at our browser we can go to localhost whatever port we're running on right now we're using the default if i put in swagger dash ui slash index.html i get my api for my tutorial so our demo application has these api calls you
Transcript Segments
[Music]
welcome today we are going to be looking
at restful api testing we're going to be
looking at using tools such as rest
assured karate
we'll be discussing things like swagger
postman
and generally just talking about web
service in general and how we go about
testing them
so the overview of this particular
course this is going to be a three-part
presentation we're going to focus on
restful apis
wrist assured
karate
the api testing tool not the actual
physical act
and we're going to look at a simple
project using both so we'll kind of do a
side-by-side comparison between
the different libraries that are out
there that we're going to test with
so let's start by talking about what is
restful api
and before i go over this let's first go
over that api is an application
programming interface
that is a set of rules that defines how
the application or device can connect to
and communicate with each other
so when we talk about rest apis we're
talking about providing a flexible
lightweight way to integrate our
applications and have emerge as the most
common method for connecting components
in a microservice architecture
now this definition comes from ibm but
typically if you search rest apis or
restful api you come up with some type
of definition similar to this and it's
essentially a way to
communicate between different
applications across the web so it's just
a
kind of a template way for us to
integrate our applications be it mobile
be it web
backend databases to web servers
in our presentation today we're
primarily going to focus on these four
http methods
now there are others and we'll look at
mozilla briefly to go over the full list
but the ones we're going to focus on are
get post put and delete
get is probably the most common http
method and it is used to request a
representation of a specified resource
so this is kind of like where we get
data or fetch data from a web service it
could be a list it could be a record
post is typically used for submitting an
entity to be a specified resource
this is where we actually have kind of
like web forms where we push a bunch of
information up
to the web servers to the application
we can also use put where we actually
replace the current representations of
the target resource with a different
payload and finally we can use delete to
remove a resource from the remote
machine
so today we're going to focus primarily
on rest assured and looking at our test
application that we're going to
integrate with
so rest assured is a java dsl and dsl
stands for domain specific
language and it's used for simplifying
tests of a rest based service built on
top of an http builder
now rest assured supports post get put
delete option patch and head
now there are other types of requests
that we can use but these are typically
the ones that are generally used to
validate and verify the response of the
requests
next we have karate and karate is
another domain specific language
and karate is an open source tool to
combine api test automation
mocks performance testing and ui
automation into a single unified
framework
one of the things about karate is that
it is built on the bdd syntax
of cucumber so it is a gherkins like
framework
and what's nice about it is
it
well it does have a java hook where you
can actually use java to expand upon
your karate test it doesn't necessarily
need java it can use a javascript and
become a kind of an open standard for
even non-programming and we'll look at
that a little bit throughout this course
finally at the end of this series we
will talk about how karate and recitard
kind of compare with one another how
they match up how they stack up
so throughout this entire presentation
we're going to primarily be doing a demo
and this demo is going to be comprised
of an application
so here is a simple overview of what it
is we're going to be looking at
so we have a company that wants to set
up a tutorial center on a university to
help students study
they have started building a web service
application that records the subjects
that will be covered
currently the interface supports
things like post get delete and put
and we're going to define some tests
that test that implementation so we'll
have things like creating a tutorial
retrieving all the tutorials in the
system retrieve a tutorial by id update
a tutorial by ib
find all published tutorials
find a title containing a string
we want to delete a specific tutorial
and then we want to delete all the
tutorials
we'll walk through how to do it through
postman we'll use swagger we'll look at
using rest assured and we'll also look
at doing this with karate
so let's get started
so to start with let's go out to the
mozilla site that i referenced in the
notes
so here we see all the different types
of http request methods that are
available to us from the web so we have
things like get head post put delete
connect options trace and patch
and a brief definition of each
next i provided and slacked the link to
rest assured so rest assured lives out
on github.com
forward slash rest dash assured forward
slash rest dash
assured
now here on the main page you have a
readme which gives you the basic
overviews of what you can do with the
language
and we'll go over this a little more
detail later
i also provide a link to the query labs
slash karate on github
and a link from the karate that kind of
gives a comparison between the two
now the karate and the comparison will
cover over the next two lessons but i
just want to provide them now if you
want to kind of read ahead before we get
to those
all right so the first thing we want to
do is we have to have some type of web
service application
so as we talked about in our slides we
have a test application already built so
i have this demo application here
that is a spring boot application with
some additional components at it so
let's look at the palm file
so it's also a maven project
so i typically use maven for all of my
web applications because it's so easy to
integrate with third-party libraries
so here in our pom file we have a list
of some dependencies now because i'm
using spring sts and i'm using
the spring framework
i can just outright import the spring
libraries that are already provided
within the spring tool suite
so that gives us access to
our spring boot starter
jpa i'm doing in kind of web driven
database here so we're going to load
things into memory we're using
h2 database and i'm using spring boot
starter web
and by doing this i have access to the
library so we can use spring boot
i also included a parent to the spring
boot library artifact which allows me to
import and set the spring boot starter
parent to 2.6.3
by doing this i do not have to specify
the versions down here it will
automatically pull in the dependencies
from this library that i want to use
all right in order to use rest assured
we have to include these libraries here
so we start with the basics i o rest
assured
and we need the rest assured artifact
if we want to use json we need to
include the json path artifact
if you want xml you want to use the xml
path
and finally if you want to do some json
scheme validations you need to include
the json scheme schema validator
so these four dependencies pretty much
wrap up everything that you need for
rest assured
now if we want to include in our project
wagger we also need to include
spring docks
prior to using spring docks you would
have to include the spring fox
swagger components but spring has now
simplified this into spring docks so for
swagger we need to use spring docks
all right so that's our palm file so
these are our dependencies
so now if we go look at the project
itself
we have
our source main java our source test
java
we also have a source main resources
folder
now because this is a spring boot
application we need to put some of our
configuration settings in the
application properties
since i have a database i need to
configure my h2
so that i can enable
the h2 in memory database and i give it
a console path so i can access this if i
want to access it from a database tool
i also need to set my data source my
class name user and password
since we're also going to use jpa to tie
into our database with our entities so
we're going to use jpa show
jpa properties hibernate dialect and
hibernate dll auto
by doing this this will show our sql in
the console down here when we actually
run
or start up spring boot
and finally to configure spider all we
had to do is add these two property
lines we had to set the spring.swagger
ui path
which is going to be api docs and we had
to do springdocs.swagger
operation sorter so how are we going to
sort our
methods
and because i did this it's going to
sort them alphabetically
the project itself is fairly
straightforward
we have our demo application so this is
our spring application so if you're
doing spring boot you just have to have
a spring boot class with main that
essentially starts
the spring application and that will be
what's down here our little spring boot
it essentially starts a tomcat web
service for us
our configuration file here i'm
pre-loading some data within our
database so i just have a command line
being that will add two tutorial records
to our database in memory every time we
restart
i find this is useful for testing
because it gives us a starting point
our web service itself is composed of a
controller
and we have these methods here we have
get all tutorials get tutorials by id
create tutorials update delete delete
all and find buy published ids
just have some basic logic in here
the interesting one is the get
we use our annotations for
our different http requests so we have
get
we have post
we have put
and delete
now this looks ugly and from a tester's
perspective we really don't need to get
into this level but i'm just kind of
walking through how the project's set up
then we have our model which contains
tutorial
and this is just our entity beam
so we define it as entity we set our
table name to tutorials in memory
and we give it an id
our title our description and our
boolean for published
and this is just a regular pojo
so we have all of our getters and
setters
and then finally we set up the
repository
and we define the repository to give it
some additional lookups we have that
find by published which returns a list
and we have find by titles containing
which also returns a list so we can look
up by
the published boolean or we could look
up by title
otherwise we use the straight up
getters and setters within our lookups
so what's interesting here so if we do
extends jpa this gives us that full kind
of crud functionality that we'll see in
our api with the addition of these two
lookups
so we talked about resources
so now let's go down and let's look at
what we have in our test
so for this particular example
since rest assured is a java library for
testing apis i thought it'd be easier to
just drop it right into our spring boot
project here
because it also gives me the ability to
reuse
our
tutorial application for our tutorial
sorry our tutorial entity beam
so here we have just a
typical spring boot test
which is essentially j unit but it's
going to kick off a spring boot instance
to run rj unit test so we can actually
deploy this test suite
as a separate microservice and run it on
a different
system so you can have your api running
on one and then kick off your test on
another we also want to add test method
order this way we can actually tell it
the order that we want to run our test
in
and then we give it the name
now in our slides we
basically stated that we want to test
these eight different components we want
to create a tutorial test
retrieve all tutorials
retrieve a tutorial by id
update
find
and delete
so the first thing we need to do is we
need to create a test
so let's come in here
so in
java
if you're using junit or test nd or most
test frameworks you have a test
annotation so we start with test
now because i want to run these in a
specific order i also can include the
order annotation
and for that it requires a number
essentially a sequential number
so we'll do one
now since we want our first test to be
create a tutorial we'll come down in
here and we'll do void
create a tutorial and let's make it
camel case
to comply with java naming conventions
so right now we have now added an empty
test to create a tutorial lookup
now before we actually continue writing
this out let's take a moment and
actually start the api look at the
console and then look at the api
structure from swagger
so let me restart spring boot
spring starts up tomcat
we initialize our h2 console for h2ui
for our database lookup
we
run our configuration so he we build our
table we be we build the create sequence
hibernate
and then we call and we insert
our tutorials so we've added we've
pre-loaded the tutorials with calculus
and how to introduce testing into the
development cycle
we have our unique ids and we have our
published boolean flag so
the calculus is published my book is not
so we have our description and we're not
published
so now that it's started if we actually
go out
and look at our browser
we can go to
localhost
whatever port we're running on right now
we're using the default
if i put in swagger dash ui
slash
index.html
i get my api for my tutorial
so our demo application has these api
calls
you