Data - Lab 1
1. Create the SimulationDataFactory
class
Steps
- Open the
pom.xml
and remove the<scope>test</scope>
for the DataFaker library - In the
com.workshop
package create a package calleddata
in thesrc/main/java
- Create a class called
SimulationDataFactory
in thecom.workshop
in thesrc/main/java
- Do the following in the class
- make the class
final
- create a private constructuor
- make the class
- Add the
Faker
object as a global attribute - Add the following code inside it
public Simulation validSimulation() { return Simulation.builder(). cpf(faker.number().digits(11)). name(faker.name().fullName()). email(faker.internet().emailAddress()). amount(new BigDecimal(faker.number().numberBetween(100, 40000))). installments(faker.number().numberBetween(2, 48)). insurance(faker.bool().bool()) .build(); }
- Open the
SimulationsTest
class - In the
shouldCreateNewSimulation()
and replace the simulation object by the usage of theSimulationDataFactory.validSimulation()
- Run the test
Expected results
- Green test execution
Solution
Click to see...
class SimulationsTest extends BaseApiConfiguration {
@Test
void shouldCreateNewSimulation() {
var simulation = SimulationDataFactory.validSimulation();
given()
.body(simulation)
.contentType(ContentType.JSON)
.when()
.post("/simulations")
.then()
.statusCode(HttpStatus.SC_CREATED)
.header("Location", CoreMatchers.containsString(simulation.getCpf()));
}
}