Skip to content

Commit a022d92

Browse files
committed
Integration test salvarCliente, invalid name, 400.
Trying to save a client with an invalid name and returns bad request.
1 parent 7d64cf8 commit a022d92

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import org.junit.jupiter.api.DisplayName;
99
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.NullAndEmptySource;
12+
import org.junit.jupiter.params.provider.ValueSource;
1013
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1215
import org.springframework.boot.test.context.SpringBootTest;
1316
import org.springframework.http.MediaType;
1417
import org.springframework.test.context.ActiveProfiles;
1518
import org.springframework.test.web.servlet.MockMvc;
1619

20+
import com.fasterxml.jackson.core.JsonProcessingException;
1721
import com.fasterxml.jackson.databind.ObjectMapper;
1822
import com.sistemacliente.SistemaClientesJavaApplication;
1923
import com.sistemacliente.model.Cliente;
@@ -75,7 +79,7 @@ public void listarClientes_emptyList_return200() throws Exception {
7579

7680
@Test
7781
@Transactional
78-
@DisplayName("Returns 200 when saving DTO client.")
82+
@DisplayName("Returns 201 when saving DTO client.")
7983
public void salvarCliente_success_return201() throws Exception {
8084
ClienteRequestDTO dto = new ClienteRequestDTO();
8185
dto.setNome("Marcus");
@@ -88,6 +92,24 @@ public void salvarCliente_success_return201() throws Exception {
8892
.andExpect(jsonPath("$.email").value("marcus@gmail.com"));
8993
}
9094

95+
@ParameterizedTest
96+
@NullAndEmptySource
97+
@ValueSource(strings = {" ", "ab", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345678901"})
98+
@DisplayName("Returns 400 when trying to save a client with an invalid name. A name is invalid if it"
99+
+" is empty, null, have less then 3 characters or more then 60 characters.")
100+
public void salvarCliente_invalidName_returns400(String name) throws JsonProcessingException, Exception {
101+
ClienteRequestDTO dto = new ClienteRequestDTO();
102+
dto.setNome(name);
103+
dto.setCpf("23501206586");
104+
dto.setEmail("marcus@gmail.com");
105+
106+
mvc.perform(post("/salvarcliente").contentType(MediaType.APPLICATION_JSON)
107+
.content(mapper.writeValueAsString(dto))).andExpect(status().isBadRequest())
108+
.andExpect(jsonPath("$.nome")
109+
.value("Nome deve ter entre 3 e 60 caracteres, não pode ser nulo ou vazio."));
110+
}
111+
112+
91113
}
92114

93115

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void salvarClientes_verboHttpIncorreto_retorno405() throws Exception {
146146
}
147147

148148
@Test
149+
@DisplayName("Returns 201 when saving DTO client.")
149150
public void salvarCliente_sucesso_retorno201() throws Exception {
150151
when(service.salvarCliente(any(ClienteRequestDTO.class))).thenReturn(cliente1);
151152

@@ -162,6 +163,8 @@ public void salvarCliente_sucesso_retorno201() throws Exception {
162163
@ParameterizedTest
163164
@NullAndEmptySource
164165
@ValueSource(strings = {" ", "ab", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345678901"})
166+
@DisplayName("Returns 400 when trying to save a client with an invalid name. A name is invalid if it"
167+
+" is empty, null, have less then 3 characters or more then 60 characters.")
165168
public void salvarCliente_nomeInvalido_retorno400(String nome) throws Exception {
166169
dto.setNome(nome);
167170

0 commit comments

Comments
 (0)