REST Assured Basics - Lab 2
In this, lab we will learn how to validate the response body.
1. Search for a CPF with restriction
List of CPF with restrictions
CPF |
---|
97093236014 |
60094146012 |
84809766080 |
62648716050 |
26276298085 |
01317496094 |
55856777050 |
55856777050 |
24094592008 |
58063164083 |
Steps
- Create a new test method in the
RestrictionsTest
class namedshouldReturnRestriction()
- Add the following to the test:
- pre-condition (
given()
) using a path parameterpathParam
using- key:
cpf
- value: any from the List of CPF with restrictions
- key:
- action (
when()
) to get (get()
) the/restrictions/{cpf}
endpoint - assert (
then()
) in the status code expecting HTTP 200- tip: use
HttpStatus.SC_OK
- add a
body()
assertion in the response body attributemessage
, usingCoreMatchers.is()
to validate the message
- tip: use
- Run the test
Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- assertion in the
message
attribute
Solution
Click to see...
2. Understand a validation exception
Steps
- In the
shouldReturnRestriction()
test, change the message inside theCoreMatchers.is()
to force a validation error - for example: remove the word
restrictions
from the assertion - Run the test
Expected result
- Test will fail, returning the following exception
Request and response logged
We added, in the BaseApiConfiguration
a code that will log in the console the request and response when any validation fails.
You will be able to see the request and response before the exception in the console.