Object Mapping - Lab 1
1. Create the Simulation object
Steps
- In the
com.workshop
package in thesrc/main/java
folder, create a package calledmodels
- Create a Java class named
Simulation
in thecom.workshop.models
package - Add the following annotation on the top of the class name, from Lombok:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
-
Add the following
private
attributes and their type:type name String
name String
cpf String
email BigDecimal
amount int
installments boolean
insurance
Expected results
Simulation
model class created within the correct annotations and attributes
Solution
Click to see...
package com.workshop.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Simulation {
private String name;
private String cpf;
private String email;
private BigDecimal amount;
private int installments;
private boolean insurance;
}
2. Create the test for the POST
request
Steps
- Open the
SimulationsTest
incom.workshop.simulation
in thesrc/test/java
folder - Create a test method named
shouldCreateNewSimulation()
- Create a
Simulation
object using the builder method - Add the precondition using the following methods:
body()
: add thesimulation
object as a parametercontentType()
add theContentType.JSON
from the REST Assured package
- Add the
post()
request for the/simulations/
endpoint
Tips
- you can use the class name, followed by the
builder()
method, then the methods. Example:Simulation.builder().
Expected results
- Test partially created with the precondition (
given()
) and request (when()
), without assertions, yet!
Solution
Click to see...
@Test
void shouldCreateNewSimulation() {
var simulation = Simulation.builder().name("Elias").cpf("123456789")
.email("elias@eliasnogueira.com").amount(new BigDecimal("3000"))
.installments(5).insurance(true).build();
given()
.body(simulation)
.contentType(ContentType.JSON)
.when()
.post("/simulations");
}
3. Addin the assertion for the POST
request
Steps
- In the
shouldCreateNewSimulation()
add the assert action (then()
):- validate the status code as 201 (CREATED)
- add the
header()
method using:Location
as the attribute to assertCoreMatchers.containsString
as the matcher, adding theCPF
value
- Run the test
Tips
- the code will look like
- you can get the
CPF
value from thesimulation
object
### Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- Header
Solution
Click to see...
@Test
void shouldCreateNewSimulation() {
var simulation = Simulation.builder().name("Elias").cpf("123456789").email("elias@eliasnogueira.com")
.amount(new BigDecimal("3000")).installments(5).insurance(true).build();
given()
.body(simulation)
.contentType(ContentType.JSON)
.when()
.post("/simulations")
.then()
.statusCode(HttpStatus.SC_CREATED)
.header("Location", CoreMatchers.containsString(simulation.getCpf()));
}