Rest Assured Workshop
Rest Assured Workshop
_REST Assured
_Hands-on exercises
Preparation
_Install a recent JDK (17)
API API
consumer provider
response
System System
A REST API request
Request headers
Request body
HTTP Resource (URI) and
method parameters
Request headers
Request body
HTTP methods
_GET, POST, PUT, PATCH, DELETE, OPTIONS, …
Resources and
method parameters
Request headers
Request body
parameters
_Uniform Resource Identifier
Resources and
method parameters
Request headers
Request body
parameters
_Path parameters
_ http://api.zippopotam.us/us/90210
_ http://api.zippopotam.us/ca/B2A
_Query parameters
_ http://md5.jsontest.com/?text=testcaseOne
_ http://md5.jsontest.com/?text=testcaseTwo
Request headers
Request body
Request headers
_Key-value pairs
Authorization: Basic
method parameters
Request headers
Request body
Authorization: Bearer
method parameters
Request headers
Request body
Request headers
Request body
Request body
_Data to be sent to the provider
_Most common:
_ JSON
_ XML
_ Plain text
Response headers
Response body
HTTP status code
Response headers
Response body
HTTP status code
_Indicates result of request processing by provider
Response headers
Response body
Response headers
_Key-value pairs
Response headers
Response body
Response body
_Data returned by the provider
_Most common:
_ JSON
_ XML
_ Plain text
Web Microservices
applications architectures
Why I ♥ testing at the API level
_Tests run much faster than UI-driven tests
_Commercial
_ Parasoft SOAtest
_ SoapUI Pro
_…
Perform an assertion on the returned response (here: on the JSON response payload)
REST Assured features
_Support for all HTTP methods (GET, POST, PUT, …)
_Support for Gherkin (Given/When/Then)
_Use of Hamcrest matchers for checks (equalTo)
_Use of Jsonpath/GPath for selecting elements from
JSON response
About Hamcrest matchers
_Express expectations in natural language
_Examples:
equalTo(X) Does the object equal X?
hasItem(“Rome”) Does the collection contain an item “Rome”?
hasSize(3) Does the size of the collection equal 3?
not(equalTo(X)) Inverts matcher equalTo()
_ http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html
About GPath
_JsonPath is a query language for JSON documents
_ REST Assured uses the GPath implementation of JsonPath
body(“address.geo.lat”, equalTo(“-37.3159”));
Validating technical response
data
_HTTP status code
_Response Content-Type header
_Other headers and their value
_Cookies and their value
_…
Logging request data
_RESTful API
Demo
_How to use the test suite
_Executing your tests
_Reviewing test results
Now it’s your turn!
_ src > test > java > exercises > RestAssuredExercises1Test.java
_Simple checks
_ Validating individual elements
_ Validating collections and items therein
_ Validating technical response properties
_Query parameters
_ http://md5.jsontest.com/?text=testcaseOne
_ http://md5.jsontest.com/?text=testcaseTwo
Parameterized testing
More efficient to do this at the API level…
_ OAuth(2)
Basic authentication
between
tests
path() takes a GPath
expression to extract
the required value
_Base URI
_Port
_Authentication data
_…
Defining and using
RequestSpecifications
(use an @ to refer to
an XML attribute)
XmlPath – examples
GraphQL
“Ask for what you need,
and get exactly that”
https://graphql.org
request (query)
GraphQL GraphQL
API API
consumer provider
response (JSON)
System System
Create a valid GraphQL query…
HashMap<String, Object>
_Capstone assignment
_Website: https://www.ontestautomation.com/training
_LinkedIn: https://www.linkedin.com/in/basdijkstra