Extra - Lab 2
In this lab, you need to refactor the SimulationsTest
placed in the com.workshop.simulation
in the src/java/test
folder.
A better approach is to comment on all the previous tests.
1. Refactor the shouldRetrieveAllSimulations()
test
Precondition
- Create a global attribute instance for the
SimulationsApiService
Steps
- In the
shouldRetrieveAllSimulations()
test, use theSimulationsApiService
to:- retrieve all the simulations
- assert if the list size is equa to or higher than 1
- Run the test
Tips
To verify the returned list size you can use the following matcher from AssertJ:
Expected results
- Green test execution
Solution
Click to see...
2. Create a test to find a simulation by name
Steps
- Create a new test method called
shouldRetrieveSimulationByItsName()
- Use the method, from the service class, which is expecting the name as a query parameter
- Assert only the name returned and for this use the following code
- Run the test
Tips
To verify the returned list size you can use the following matcher from AssertJ:
Expected results
- Green test execution
Solution
Click to see...
3. Create a test to find a simulation by cpf
Steps
- Create a new test method called
shouldFindSimulationByCpf()
- Use the method, from the service class, which is expecting the
cpf
as path parameter - Assert all the data, including the
id
, using the following code snippet - Run the test
Soft assertions
AssertJ has a feature called Soft Assertions
It will assert all data inside the assertSoftly
method, not stopping the execution when the assertion fails.
Tips
Use the following assertions methods to replace the assertMethod()
in step 3:
isNotNull()
for theid
assertionisEqualTo()
for all text assertionsisTrue()
orisFalse()
for the boolean assertion
Expected results
- Green test execution
Solution
Click to see...
@Test
void shouldFindSimulationByCpf() {
var simulation = simulationsApiService.retrieveSimulation("66414919004");
SoftAssertions.assertSoftly(softly -> {
Assertions.assertThat(simulation.getId()).isNotNull();
Assertions.assertThat(simulation.getName()).isEqualTo("Tom");
Assertions.assertThat(simulation.getCpf()).isEqualTo("66414919004");
Assertions.assertThat(simulation.getEmail()).isEqualTo("tom@gmail.com");
Assertions.assertThat(simulation.getAmount()).isEqualTo("11000.00");
Assertions.assertThat(simulation.getInstallments()).isEqualTo(3);
Assertions.assertThat(simulation.isInsurance()).isTrue();
});
}
4. Create a test to create a new simulation
Steps
- Create a new test method called
shouldCreateSimulation()
- Create a
Simulation
object to use as the request body, with any data you like inside - Assert the return, which is a
Header
using thegetValue()
and thecontains()
assertion to see if the cpf is present in the location - Run the test
Expected results
- Green test execution
Solution
Click to see...
@Test
void shouldCreateSimulation() {
var simulation = Simulation.builder().name("Robert").cpf("98765432103").email("robert@gmail.com")
.amount(new BigDecimal("3000.00")).installments(5).insurance(true).build();
var header = simulationsApiService.createSimulation(simulation);
Assertions.assertThat(header.getValue()).contains(simulation.getCpf());
}
5. Create a test to update an existing simulation
Steps
- Create a new test method called
shouldUpdateExistingSimulation()
- Create a
Simulation
object to use as the request body, with any data you like inside - Create the simulation using the
createSimulation()
method from the service class - Change the name of the Simulation created in step 2
- Update the Simulation using the
cpf
from the object created in step 2 - Assert that the Simulation object from step 2 is equal to the one returned from the updated in step 5
- Run the test
Expected results
- Green test execution
Solution
Click to see...
@Test
void shouldUpdateExistingSimulation() {
var newSimulation = Simulation.builder().name("Robert").cpf("748392749450").email("robert@gmail.com")
.amount(new BigDecimal("3000.00")).installments(5).insurance(true).build();
simulationsApiService.createSimulation(newSimulation);
newSimulation.setName("Different name");
var simulationUpdated = simulationsApiService.updateSimulation(newSimulation.getCpf(), newSimulation);
Assertions.assertThat(simulationUpdated).isEqualTo(newSimulation);
}
6. Create a test to delete an existing simulation
Steps
- Create a new test method called
shouldDeleteExistingSimulation()
- Create a
Simulation
object to use as the request body, with any data you like inside - Create the simulation using the
createSimulation()
method from the service class - Delete the simulation created using the delete method from the service class, using the cpf from the simulation created
- Assert that the return is
true
Expected results
- Green test execution
Solution
Click to see...
@Test
void shouldDeleteExistingSimulation() {
var simulation = Simulation.builder().name("Robert").cpf("874222357").email("robert@gmail.com")
.amount(new BigDecimal("3000.00")).installments(5).insurance(true).build();
simulationsApiService.createSimulation(simulation);
var isDeleted = simulationsApiService.deleteSimulation(simulation.getCpf());
Assertions.assertThat(isDeleted).isTrue();
}