11package com .example .cp_main_be .user .presentation ;
22
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
4+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
7+
38import com .example .cp_main_be .config .AbstractContainerBaseTest ;
49import com .example .cp_main_be .user .domain .User ;
510import com .example .cp_main_be .user .domain .repository .UserRepository ;
611import com .example .cp_main_be .user .dto .request .UserRequest ;
712import com .fasterxml .jackson .databind .ObjectMapper ;
13+ import java .util .UUID ;
814import org .junit .jupiter .api .DisplayName ;
915import org .junit .jupiter .api .Test ;
1016import org .springframework .beans .factory .annotation .Autowired ;
1521import org .springframework .test .web .servlet .ResultActions ;
1622import org .springframework .transaction .annotation .Transactional ;
1723
18- import java .util .UUID ;
19-
20- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
21- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
22- import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
23- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
24-
2524@ SpringBootTest
2625@ AutoConfigureMockMvc
2726@ Transactional
2827class UserControllerTest extends AbstractContainerBaseTest {
2928
30- @ Autowired
31- private MockMvc mockMvc ;
29+ @ Autowired private MockMvc mockMvc ;
3230
33- @ Autowired
34- private ObjectMapper objectMapper ;
31+ @ Autowired private ObjectMapper objectMapper ;
3532
36- @ Autowired
37- private UserRepository userRepository ;
33+ @ Autowired private UserRepository userRepository ;
3834
39- @ DisplayName ("닉네임을 받아 유저를 등록한다." )
40- @ Test
41- void register () throws Exception {
42- // given
43- UserRequest userRequest = new UserRequest (UUID .randomUUID (),1L ,"test" );
44- String requestBody = objectMapper .writeValueAsString (userRequest );
35+ @ DisplayName ("닉네임을 받아 유저를 등록한다." )
36+ @ Test
37+ void register () throws Exception {
38+ // given
39+ UserRequest userRequest = new UserRequest (UUID .randomUUID (), 1L , "test" );
40+ String requestBody = objectMapper .writeValueAsString (userRequest );
4541
46- // when
47- ResultActions result = mockMvc .perform (post ("/api/v1/register/nickname" )
42+ // when
43+ ResultActions result =
44+ mockMvc .perform (
45+ post ("/api/v1/register/nickname" )
4846 .contentType (MediaType .APPLICATION_JSON )
4947 .content (requestBody ));
5048
51- // then
52- result .andDo (print ())
53- .andExpect (status ().isOk ())
54- .andExpect (jsonPath ("$.success" ).value (true ))
55- .andExpect (jsonPath ("$.data.username" ).value ("test" ))
56- .andExpect (jsonPath ("$.data.uuid" ).isNotEmpty ());
57- }
58-
59- @ DisplayName ("UUID로 유저 정보를 조회한다." )
60- @ Test
61- void getUserInfo_success () throws Exception {
62- // given
63- // 테스트를 위해 미리 유저를 한 명 저장
64- User savedUser = userRepository .save (User .builder ().username ("existing-user" ).build ());
65- UUID userUuid = savedUser .getUuid ();
66-
67- // when
68- ResultActions result = mockMvc .perform (get ("/api/v1/users/{uuid}" , userUuid ));
69-
70- // then
71- result .andDo (print ())
72- .andExpect (status ().isOk ())
73- .andExpect (jsonPath ("$.success" ).value (true ))
74- .andExpect (jsonPath ("$.data.username" ).value ("existing-user" ))
75- .andExpect (jsonPath ("$.data.uuid" ).value (userUuid .toString ()));
76- }
77-
78- @ DisplayName ("존재하지 않는 UUID로 유저 정보를 조회하면 404 에러가 발생한다." )
79- @ Test
80- void getUserInfo_fail_whenUserNotFound () throws Exception {
81- // given
82- UUID nonExistentUuid = UUID .randomUUID ();
83-
84- // when
85- ResultActions result = mockMvc .perform (get ("/api/v1/users/{uuid}" , nonExistentUuid ));
86-
87- // then
88- result .andDo (print ())
89- .andExpect (status ().isNotFound ()); // UserNotFoundException이 404로 변환되는지 확인
90- }
91- }
49+ // then
50+ result
51+ .andDo (print ())
52+ .andExpect (status ().isOk ())
53+ .andExpect (jsonPath ("$.success" ).value (true ))
54+ .andExpect (jsonPath ("$.data.username" ).value ("test" ))
55+ .andExpect (jsonPath ("$.data.uuid" ).isNotEmpty ());
56+ }
57+
58+ @ DisplayName ("UUID로 유저 정보를 조회한다." )
59+ @ Test
60+ void getUserInfo_success () throws Exception {
61+ // given
62+ // 테스트를 위해 미리 유저를 한 명 저장
63+ User savedUser = userRepository .save (User .builder ().username ("existing-user" ).build ());
64+ UUID userUuid = savedUser .getUuid ();
65+
66+ // when
67+ ResultActions result = mockMvc .perform (get ("/api/v1/users/{uuid}" , userUuid ));
68+
69+ // then
70+ result
71+ .andDo (print ())
72+ .andExpect (status ().isOk ())
73+ .andExpect (jsonPath ("$.success" ).value (true ))
74+ .andExpect (jsonPath ("$.data.username" ).value ("existing-user" ))
75+ .andExpect (jsonPath ("$.data.uuid" ).value (userUuid .toString ()));
76+ }
77+
78+ @ DisplayName ("존재하지 않는 UUID로 유저 정보를 조회하면 404 에러가 발생한다." )
79+ @ Test
80+ void getUserInfo_fail_whenUserNotFound () throws Exception {
81+ // given
82+ UUID nonExistentUuid = UUID .randomUUID ();
83+
84+ // when
85+ ResultActions result = mockMvc .perform (get ("/api/v1/users/{uuid}" , nonExistentUuid ));
86+
87+ // then
88+ result .andDo (print ()).andExpect (status ().isNotFound ()); // UserNotFoundException이 404로 변환되는지 확인
89+ }
90+ }
0 commit comments