In my very
popular Tester’s toolbox - an alternative guide
I listed few examples of free public APIs that can be used for learning Rest Assured. Recently I have found one more
site - www.jsontest.com. Having in mind that my
last API test post was almost
exactly one year ago I decided to utilize my latest finding and show you few
examples. JSON Test API has few excellent functionalities for people who want to learn.
In order to use Rest Assured in your Maven project add this dependency:
Rest Assured GET examples
As a nice beginning I suggest to test ip.jsontest.com. This endpoint prints your current IP
address. Here is simple test case that looks pretty much like copy/paste from
excellent getting started guide. Please note that
you would most likely have to update partOfMyIp String.
Second endpoint - date.jsontest.comreturns date in three formats. Dealing with a time in
tests can be tricky (flaky) so I decided to assert only date in MM-dd-yyyy format.
Another endpoint - cookie.jsontest.comsets the cookie as response. Here is the test that
asserts if the cookie was set.
The most fun you may get from headers.jsontest.com. This endpoint reads headers from your
request and prints them in response. I decided to send XML with UTF-8 enconding and assert Content Type.
Rest Assured POST examples
Those were GET examples. Now we would use POST method and send some data. Please note that .queryParam() method has to
be defined in given() section which is a little bit strange for me. I’d rather code that in when() section after
providing url.
First endpoint -md5.jsontest.com/?text=texttakes your text and returns it together
with calculated md5 checksum.
Final endpoint which I decided to test isvalidate.jsontest.com. It takes ‘json’
queryParam value and validates if it’s parse-able (i.e. if it’s correctly formatted json). Below is simple happy path: