77
88import org .junit .jupiter .api .DisplayName ;
99import 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 ;
1013import org .springframework .beans .factory .annotation .Autowired ;
1114import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
1215import org .springframework .boot .test .context .SpringBootTest ;
1316import org .springframework .http .MediaType ;
1417import org .springframework .test .context .ActiveProfiles ;
1518import org .springframework .test .web .servlet .MockMvc ;
1619
20+ import com .fasterxml .jackson .core .JsonProcessingException ;
1721import com .fasterxml .jackson .databind .ObjectMapper ;
1822import com .sistemacliente .SistemaClientesJavaApplication ;
1923import 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
0 commit comments