REST Assured Basics - Lab 3
1. Create the package to add the restrictions tests
- Create an additional package called
simulation
incom.workshop
package in thesrc/test/java
folder
2. Retrieve all records from the Simulation API
Steps
- Create a Java class named
SimulationsTest
incom.workshop.simulation
in thesrc/test/java
folder - Make
SimulationsTest
extendsBaseApiConfiguration
- Create a test method named
shouldRetrieveAllSimulations()
- Add the following to the test:
- action (
when()
) to get (get()
) the/simulations/
endpoint - assert (
then()
) in the status code expecting HTTP 200- tip: use
HttpStatus.SC_OK
- add a
body()
assertion in the response body for each attribute usingCoreMatchers.is()
to validate values returned
- tip: use
- action (
- Run the test
Tips
- don't forget to set the array position
[0].
- for the
id
attribute use theCoreMatchers.notNullValue()
assertion - for the
amount
attribute, use the value asBigDecimal
- example:
new BigDecimal("11000.00")
Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- assertion in all the attributes
Solution
Click to see...
@Test
void shouldRetrieveAllSimulations() {
when()
.get("/simulations/")
.then()
.statusCode(HttpStatus.SC_OK)
.body("[0].id", CoreMatchers.notNullValue())
.body("[0].name", CoreMatchers.is("Tom"))
.body("[0].cpf", CoreMatchers.is("66414919004"))
.body("[0].email", CoreMatchers.is("tom@gmail.com"))
.body("[0].amount", CoreMatchers.is(new BigDecimal("11000.00")))
.body("[0].installments", CoreMatchers.is(3))
.body("[0].insurance", CoreMatchers.is(true));
}
3. Retrieve all records and assert their size
Steps
- In the
SimulationsTest
class, create a test method namedshouldRetrieveAllSimulationsCheckingSize()
- Add the same actions from the previous test, making a
GET
request to/simulations/
- In the assertion, use
$
instead of an attribute name and useMatchers.hasSize()
to assert the number of elements returned - Run the test
Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- assertion in the array size
Solution
Click to see...
4. Using soft assertions
Steps
- In the
SimulationsTest
class, modify theshouldRetrieveAllSimulations()
to have the soft assertions approach - Make a temporary change in the following attribute values
name
toUnknown
installments
to0
- Run the test
Expected results
- The test execution will fail with the following error in the console
- Rever the data changes