Skip to content

Commit 7d64cf8

Browse files
committed
Integration test salvarCliente returns 201
1 parent 8d67fe3 commit 7d64cf8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/test/java/com/sistemaclliente/ClienteControllerIntegrationTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sistemaclliente;
22

33
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
45
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
56
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
67

@@ -9,12 +10,14 @@
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1112
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.http.MediaType;
1214
import org.springframework.test.context.ActiveProfiles;
1315
import org.springframework.test.web.servlet.MockMvc;
1416

1517
import com.fasterxml.jackson.databind.ObjectMapper;
1618
import com.sistemacliente.SistemaClientesJavaApplication;
1719
import com.sistemacliente.model.Cliente;
20+
import com.sistemacliente.model.dto.ClienteRequestDTO;
1821
import com.sistemacliente.model.dto.ClienteResponseDTO;
1922
import com.sistemacliente.repository.ClienteRepository;
2023

@@ -39,8 +42,8 @@ public void contextLoads(){}
3942

4043
@Test
4144
@Transactional
42-
@DisplayName("Retorna 200 e lista de todos os clientes do banco de dados.")
43-
public void listarClientes_listaCheia_retornar200() throws Exception {
45+
@DisplayName("Returns 200 and a list of the clients from the database.")
46+
public void listarClientes_fullList_return200() throws Exception {
4447
repository.deleteAll();
4548

4649
Cliente cliente1 = new Cliente();
@@ -63,13 +66,28 @@ public void listarClientes_listaCheia_retornar200() throws Exception {
6366

6467
@Test
6568
@Transactional
66-
@DisplayName("Retorna 200 e lista vazia, pois o banco foi limpo.")
67-
public void listarClientes_listaVazia_retorno200() throws Exception {
69+
@DisplayName("Returns 200 and an empty list because we clened the database.")
70+
public void listarClientes_emptyList_return200() throws Exception {
6871
repository.deleteAll();
6972
mvc.perform(get("/listarclientes")).andExpect(status().isOk())
7073
.andExpect(jsonPath("$.length()").value(0));
7174
}
7275

76+
@Test
77+
@Transactional
78+
@DisplayName("Returns 200 when saving DTO client.")
79+
public void salvarCliente_success_return201() throws Exception {
80+
ClienteRequestDTO dto = new ClienteRequestDTO();
81+
dto.setNome("Marcus");
82+
dto.setCpf("23501206586");
83+
dto.setEmail("marcus@gmail.com");
84+
85+
mvc.perform(post("/salvarcliente").content(mapper.writeValueAsString(dto))
86+
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isCreated())
87+
.andExpect(jsonPath("$.nome").value("Marcus")).andExpect(jsonPath("$.cpf").value("23501206586"))
88+
.andExpect(jsonPath("$.email").value("marcus@gmail.com"));
89+
}
90+
7391
}
7492

7593

@@ -90,6 +108,5 @@ public void listarClientes_listaVazia_retorno200() throws Exception {
90108

91109

92110

93-
94111

95112

src/test/java/com/sistemaclliente/ClienteControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void salvarClientes_verboHttpIncorreto_retorno405() throws Exception {
146146
}
147147

148148
@Test
149-
public void salvarCliente_sucesso_retorno200() throws Exception {
149+
public void salvarCliente_sucesso_retorno201() throws Exception {
150150
when(service.salvarCliente(any(ClienteRequestDTO.class))).thenReturn(cliente1);
151151

152152
mvc.perform(post("/salvarcliente").contentType(MediaType.APPLICATION_JSON)

0 commit comments

Comments
 (0)