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] now if we do this side by side you see we have two deletes we have three gets we have our post and our put now with swagger this is really cool so if you have swagger installed in your web service application not only does it give you a definitions page to where you can come in and actually look and see how the api works which eliminates the need to really go back and know what the code does because in most web service integrations you're going to be provided this api definition but not the actual source code so you really don't know what's going on behind the scenes what type of technologies they have running if it's you know c sharp java php python you have no idea but what you do have is a definition and you know that using the http request methods you can actually call this api from your application you can call it from whatever language you want to call it from you could also call it from web services a standalone application or even a mobile application so with swagger it defines our apis for us but what swiger also gives us is it gives us the capability to test our apis directly from this interface for instance if i come down here to our get api tutorials if i expand this out it gives us the definitions we need to interface with tutorials so here we see we can pass a parameter title and we should get back a response of 200. we will also get back some type of json so it gives us our example output here but what's really cool is if this output ties to like a dto an entity being or some type of object you can click the schema then you can actually see what this json structure looked like so it's going to be a list of tutorials and our tutorial contains our id our title our description and publish so now you know what type of pojo to build on your side if you want to map this json object using something like jackson or a json map so you don't have to actually work directly with the json you could actually work with an actual object the other cool thing about swagger is you can actually test directly from swagger so i can hit try out and since title is optional we can just click the execute button and this basically acts like a waveform so we just called we just did a get call to localhost 880 api tutorials and we also passed in the parameter to accept any type of response here is our request url so for testing purposes this is the url we want to test with and here is the output so we see we got our two records we got our calculus math true and we got our second record how to introduce testing into the development life cycle our description and false you can also download this and it also gives you the response headers so for testing purposes you want to make sure if you have to pass any data in a specific form json or html or xml you in your request header may have to pass a different context type so if we were to take this and open up say postman we could do this exact same test from within postman so in postman if i just create a new request paste in my url i do a get my headers don't need any specific headers or parameters authorizations or body and i just click send and as you can see we get the same output now what's neat with postman is if we go to our console if we expand out on the console we also see that we get the exact same information that we can get from within swagger so we have a request header response header response body with our json if we had any request contacts we could pass that as well and it gives us some additional network information the reason i'm showing you both cases is because from rest assured in karate we get the same information we can parse this we can test this we can build this all this is provided within those libraries all right so let's go out to our test case so in order to use rest assured it's actually pretty simple so once you have the library installed you just simply start typing rest assured dot and now we have a whole bunch of different configuration methods that we can work with so for testing purposes we want to uh so it's actually rest assured is actually kind of built using the behavior like syntax of given then assert body you have response you have status code so basically most of the typical behavior driven testing given when then is how you define your rest assured calls and what's interesting is rest assured uses their chaining method approach to apply everything in one single line so you can actually do everything in one call so we can do rest assured dot given returns us a request specification so we need to given we can pass parameters we can pass in headers so we'll start with given and we'll kind of break this out so it's easy to read so we got given now for our test purposes for this example i also want to implement the log feature so if i use the log method it will get the request log specifications and i can specify the type of logging i want to write out to the console so you can actually write out the cookies you can write out for testing many different components of the request header or of the request itself i'm just going to use all so we can kind of see everything that comes back so we have that next we need to have our uri so what i need to do here is i need to call the method base uri and this provides the host information for our api call now since we're using one particular api for everything we can define a public static string and we can just use uri and we can set it equal to http localhost 88 so we'll take our uri and we will drop it into our base uri here so now all of our calls will use the base http localhost 8080. what's interesting about using this approach is you can define environment variables you could create an environment json over here and inside your test you could pass in the environment variables be it qa dev prod and then go read that json value from the host from your resource versus hard coding this into your application so now we get away from that test driven development and behavior driven development and get more into that hybrid approach using data driven development so we have base uri and we know the context text type so the first one we're actually going to do is we're going to create the tutorial so this is actually actually let me jump ahead a little bit so let's actually retrieve all tutorials let's actually do the second one first only reason i want to do the second one first is because it's a very simple call all right so if we do the second one which is retrieve all tutorials and we'll code it so in here we're going to do that get call so we need to call the base url we don't need the context type in this case because it's just a simple get call so we have given we're setting our logs we're setting our host now we're going to do when so given our url when we get so now under when you can do when accept body cookies delete so under when is where we set all of our headers our form parameters and we set the specific http request type so in this case we're going to do get and we need to specify the api so now api all right so now we have our given and our when now we need to follow that up with then so given our host given some setup when we want to get a api maybe pass in some parameters some context set some headers then what do we do when it comes back so then we can log our response so we again we have our log so that under given we're logging the request under then we're logging the response log all so if i leave it here this will run it will retrieve the json and just write it to the logs however i kind of wanted to add one additional step here because we know from swagger when we call this guy when we call this get or api tutorials we are supposed to get a response code of 200. so what we can do here in our then we can then say status code and we can tell it what to expect 200. so if we do not get 200 this step should fail so if i save this all right so we have our spring boot test we have our test method order and we have our second test to retrieve all tutorials so to run this all we have to do is go to our class over here on the left or within the code right click run as junit test and in our console here it will start a spring boot microservice for our test case and in our window in our junit window it called rest assured demo application test and it called retrieve all tutorials and it passed so let's go look at the actual log and see what we got in the console so in our console here it started our boot it deployed our test it ran our test so it reset spring boot it defined our calculus and our books so then we ran our test so our request method was get our request uri was localhost api tutorials we set no proxy we had no request parameters no query form or path parameters our header was accept all we had no cookies multi-part or body we use http 1 to receive 200 and the response came back as json and we wrote the json that came back ironically enough this looks identical to what we see in both postman and in or in swagger and in postman so real simple real clean real easy to follow so if you're already using java rest assured really does seem to be the way to go it's real clean real straightforward right so that's just retrieving the tutorials pretty easy pretty straightforward we have a given base uri when we get something then check the status code now what's cool about this is if you look at this last part here so if you do rest assured given and then you follow all the way down to then it returns a valid response so this guy actually returns a response and it will be an io rest assured response class there we go so this returns the response and then from your response object you can do return you can do pretty print you can get the status code so you can now do everything that you can also do here with the chaining of methods what's nice about this is this gives you the ability to expand upon the validation that you can't do with chaining so you could do something like get body dot as pretty string and you can print that out or you could take this and map this to an object and we'll get to that in just a minute so for now let's just put this back all right so let's go back up to our first test and now let's try to create the object so when we do the create let's go look at swagger so let's look at creating a tutorial so to create a tutorial we need to do the post request here and here in swagger it tells us oh okay a request has no parameters but it requires a request body of application json with the schema of our tutorial so we have to pass it in a id titled description and publish now since we're in our actual application this makes it simple because we can just reuse our entity now here the response will also come back as just the tutorial so it will show that it was created all right so let's go look at how we do this so we have rest assured we have given we have base uri but we need to set the context type in this section here to match what we saw in swagger so here our contact site needs to be application json okay application json so this matches what we have in swagger and we need to pass in a body so now we have our body and we need to give it an object now this object from looking over here needs to be a tutorial object so we need to pass in a tutorial json well since we know the schema and we're already in our boot application we can just cheat and reuse tutorial so here we can do new tutorial and we can use the constructor of tutorial to set the title description and if it was published the detector's guide to the galaxy space travel and it is published so we have set our context type to application json we're setting our body we're using new tutorial and this will create our entity beam and it will also set it to json when it passes it up it'll convert our object to json really clean really straightforward code is not cluttered all right so we have our body so we've essentially now set up in the given everything we need to do to submit the information we need to the call so next we do then or i'm sorry when so given all of our setup when now in here let's look at swagger again so we see here this has to be a post request so now we're going to do post and our post needs to be the api tutorials so we come back over here so we see post here is our post api so api tutorials we now have our header we have our when and we are doing a post then let's log what we get back let's log everything and again let's check for status of 200 which if we look at swagger that's what we should get from our post so post our parameters parameters in the body and our response should be 200 and we should get back our json object with our record id so let me save this and we will rerun this you
Transcript Segments
[Music]
now if we do this side by side
you see we have
two deletes
we have three gets
we have our
post and our put
now with swagger this is really cool
so if you have swagger installed in your
web service application
not only does it give you a definitions
page to where you can come in and
actually look and see how the api works
which eliminates the need to really
go back and know what the code does
because in most web service integrations
you're going to be provided
this api definition but not the actual
source code so you really don't know
what's going on behind the scenes what
type of technologies they have running
if it's
you know c sharp java php python
you have no idea
but what you do have is a definition and
you know that using the http request
methods you can actually call this api
from
your application you can call it from
whatever language you want to call it
from
you could also call it from web services
a standalone application or even a
mobile application
so with swagger
it defines our apis for us but what
swiger also gives us is it gives us the
capability to test our apis directly
from this interface for instance if i
come down here to
our get api tutorials
if i expand this out
it gives us the definitions we need to
interface with tutorials so here we see
we can pass a parameter title
and we should get back a response of
200.
we will also get back some type of json
so it gives us our example
output here
but what's really cool is if this output
ties to like a dto an entity being or
some type of object
you can click the schema
then you can actually see what this json
structure looked like so it's going to
be a list of tutorials
and our tutorial contains our id our
title our description and publish
so now you know what type of pojo to
build on your side if you want to map
this json object using something like
jackson or a json map
so you don't have to actually work
directly with the json you could
actually work with an actual object
the other cool thing about swagger is
you can actually test directly from
swagger
so i can hit try out
and since title is optional we can just
click the execute button
and this basically acts like a waveform
so we just called we just did a get call
to localhost 880 api tutorials
and we also passed in the parameter to
accept any type of response
here is our request url
so for testing purposes this is the url
we want to test with
and here is the output so we see we got
our two records we got our calculus math
true
and we got our second record how to
introduce testing into the development
life cycle our description and false
you can also download this and it also
gives you the response headers
so for testing purposes you want to make
sure if you have to pass any data
in a specific form
json
or html or xml you in your request
header may have to pass a different
context type
so if we were to take this
and open up say postman
we could do this exact same test from
within postman
so in postman if i just create a new
request
paste in my url
i do a get
my headers don't need any specific
headers or parameters authorizations or
body
and i just click send
and as you can see we get the same
output
now what's neat with postman is if we go
to our console
if we expand out on the console we also
see that we get the exact same
information that we can get from within
swagger so we have a request header
response header
response body
with our json if we had any request
contacts we could pass that as well and
it gives us some additional network
information
the reason i'm showing you both cases is
because from rest assured in karate we
get the same information we can parse
this we can test this we can build this
all this is provided within those
libraries
all right so let's go out to our test
case
so in order to use rest assured
it's actually pretty simple so once you
have the library installed you just
simply start typing
rest
assured
dot
and now we have a whole bunch of
different configuration methods that we
can work with
so for testing purposes we
want to uh so it's actually rest assured
is actually kind of built using the
behavior
like
syntax of given then assert
body
you have response you have status code
so basically most of the
typical behavior driven
testing given when then
is how you define
your rest assured calls
and what's interesting is rest assured
uses their chaining method approach
to apply everything in one single line
so you can actually do
everything in one call so we can do rest
assured dot given
returns us a request specification
so we need to given we can pass
parameters we can pass in headers
so we'll start with given
and we'll kind of break this out so it's
easy to read so we got given
now
for our test purposes for this example
i also want to implement the log feature
so if i use the log method
it will get the request log
specifications
and i can specify the type of logging i
want to write out to the console
so you can actually write out the
cookies you can write out
for testing many different components of
the request header or of the request
itself
i'm just going to use all so we can kind
of see everything that comes back
so we have that
next
we need to have our uri
so what i need to do here is i need to
call the method
base
uri
and this provides the host information
for our api call
now since we're using one particular api
for everything we can define
a public
static
string
and we can just use uri
and we can set it equal
to http
localhost
88
so we'll take our uri
and we will drop it into our base uri
here
so now all of our calls will use the
base http localhost 8080.
what's interesting about using this
approach is you can define environment
variables
you could create an environment json
over here
and inside your test you could pass in
the environment variables be it qa dev
prod and then go read that json value
from the host from your resource
versus hard coding this into your
application
so now we get away from that test driven
development and behavior driven
development and get more into that
hybrid approach using data driven
development
so we have base uri
and we know
the context
text type
so the first one we're actually going to
do is we're going to create the tutorial
so this is actually
actually let me jump ahead a little bit
so let's actually retrieve all tutorials
let's actually do the second one first
only reason i want to do the second one
first is because it's a very simple call
all right so if we do the second one
which is retrieve all tutorials
and we'll code it
so in here we're going to do that get
call so we need to call the base url we
don't need the context type in this case
because it's just a simple get call
so we have given we're setting our logs
we're setting our host
now we're going to do when
so given our url
when
we get
so now under when you can do when
accept body
cookies
delete
so under when is where we set all of our
headers our form parameters and we set
the specific
http request type so in this case we're
going to do get
and we need to specify the api
so now api
all right so now we have our given
and our when
now we need to follow that up with then
so given
our host given some setup when we want
to get a api
maybe pass in some parameters some
context set some headers
then what do we do when it comes back
so then
we can log
our response
so we again we have our log so that
under given we're logging the request
under then we're logging the response
log all
so if i leave it here this will run it
will retrieve the json and just write it
to the logs
however i kind of wanted to add one
additional step here
because we know
from swagger
when we call this guy when we call this
get or api tutorials we are supposed to
get a response code of 200.
so what we can do here in our then
we can then say
status code
and we can tell it what to expect
200. so if we do not get 200 this step
should fail
so if i save this
all right so we have our spring boot
test we have our test method order
and we have our second test
to retrieve all tutorials
so to run this all we have to do is go
to our class
over here on the left or within the code
right click
run as
junit test
and in our console here it will start a
spring boot
microservice for our test case
and in our window
in our junit window it called rest
assured demo application test
and it called retrieve all tutorials
and it passed
so let's go look at the actual log and
see what we got in the console so in our
console here
it started our boot
it deployed our test
it ran our test
so it reset spring boot it defined our
calculus
and our books
so then we ran our test so our request
method was get
our request uri was localhost api
tutorials we set no proxy we had no
request parameters no query form or path
parameters
our header was accept all
we had no cookies multi-part or body
we use http 1
to receive 200
and the response came back as json
and we
wrote the json that came back
ironically enough this looks identical
to what we see in both postman
and
in or in swagger and
in postman
so real simple real clean real easy to
follow so if you're already using java
rest assured really does seem to be the
way to go it's real clean real
straightforward
right so that's just retrieving the
tutorials pretty easy pretty
straightforward we have a given base uri
when we get something then
check the status code
now what's cool about this is if you
look at this last part here so if you do
rest assured
given
and then you follow all the way down to
then
it returns a valid response
so this guy actually returns
a response
and it will be an io rest assured
response class
there we go
so this returns the response and then
from your response
object you can do
return you can do pretty print
you can get the status code so you can
now do everything that you can also do
here with the chaining of methods
what's nice about this is this gives you
the ability to expand upon the
validation that you can't do with
chaining so you could do something like
get body
dot as pretty string
and you can print that out
or you could take this and map this to
an object
and we'll get to that in just a minute
so for now let's just put this back
all right so let's go back up to our
first test
and now let's try to create the object
so when we do the create
let's go look at swagger
so let's look at creating a tutorial so
to create a tutorial we need to do the
post request here
and here
in swagger it tells us oh okay a request
has no parameters
but it requires a request body of
application json
with the schema
of our tutorial so we have to pass it in
a
id titled description and publish
now since we're in our actual
application
this makes it simple because we can just
reuse our entity
now here the response will also come
back as just the tutorial so it will
show that it was created
all right so let's go look at how we do
this
so we have rest assured we have given we
have base uri but we need to set the
context type in this section here to
match what we saw in swagger
so here our contact site needs to be
application json
okay application json so this matches
what we have in swagger
and we need to pass in a body so now we
have our body
and we need to give it an object now
this object
from looking over here
needs to be
a tutorial object so we need to pass in
a tutorial json
well since we know the schema and we're
already in our boot application we can
just cheat and reuse tutorial
so here we can do new
tutorial
and we can use the constructor of
tutorial to set the title description
and if it was published
the detector's guide to the galaxy
space travel
and it is published
so we have set our context type to
application json
we're setting our body we're using new
tutorial
and this will create our entity beam and
it will also set it to json when it
passes it up it'll convert our object to
json
really clean really straightforward code
is not cluttered
all right so we have our body
so we've essentially now set up in the
given everything we need to do
to submit the information we need to the
call so next we do then
or i'm sorry when
so given
all of our
setup
when
now in here let's look at swagger again
so we see here this has to be a post
request
so now we're going to do post
and our post needs to be
the api tutorials so we come back over
here so we see post here is our post api
so api tutorials
we now have our header
we have our when
and we are doing a post
then
let's log
what we get back
let's log everything
and again let's check for status
of 200
which if we look at swagger that's what
we should get from our post
so post our parameters parameters in the
body
and our response should be 200 and we
should get back
our json object with our record id
so let me save this
and we will rerun this
you