Object Mapping - Lab 3
1. Using object deserialization
Steps
- In the
shouldUpdateExistingSimulation()
remove thebody()
method - Add the method
extract().as()
, adding theSimulation.class
as a parameter for the methodas()
- Create an object called
simulationUpdated
and associate it with thegiven()
method - Add an assertion using the AssertJ library that will verify if the simulation created is equal to the simulation updated
- Run the test
Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- response body
Info
Note that we are verifying two objects using AssertJ.
When one of the values in an attribute is not equal, AssertJ will show the attribute that does not matches.
This approach adds an elegant, less code, and modern solution to the assertion.
Solution
Click to see...
@Test
void shouldUpdateExistingSimulation() {
String existingCpf = "17822386034";
var simulation = Simulation.builder().name("Elias").cpf("17822386034").email("elias@eliasnogueira.com")
.amount(new BigDecimal("3000.00")).installments(5).insurance(true).build();
var simulationUpdated =
given()
.pathParam("cpf", existingCpf)
.body(simulation)
.contentType(ContentType.JSON)
.when()
.put("/simulations/{cpf}")
.then()
.statusCode(HttpStatus.SC_OK)
.extract().as(Simulation.class);
Assertions.assertThat(simulationUpdated).isEqualTo(simulation);
}