22
33import com .ai .lawyer .domain .poll .service .PollService ;
44import org .junit .jupiter .api .Test ;
5+ import org .junit .jupiter .api .BeforeEach ;
56import org .mockito .Mockito ;
67import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
9+ import org .springframework .test .context .bean .override .mockito .MockitoBean ;
810import org .springframework .test .web .servlet .MockMvc ;
911import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
1012import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
11- import org .springframework .security .core .Authentication ;
12- import org .springframework .security .core .context .SecurityContextHolder ;
13- import org .springframework .security .core .context .SecurityContext ;
14- import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1513import com .ai .lawyer .domain .poll .dto .PollDto ;
1614import com .ai .lawyer .domain .poll .dto .PollVoteDto ;
17- import com .fasterxml .jackson .databind .ObjectMapper ;
18- import org .mockito .Mock ;
19- import org .springframework .boot .test .mock .mockito .MockBean ;
2015import org .springframework .context .annotation .Import ;
2116import com .ai .lawyer .global .security .SecurityConfig ;
22- import org .springframework .security .test .context .support .WithMockUser ;
2317import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
2418import org .junit .jupiter .api .DisplayName ;
19+ import jakarta .servlet .http .Cookie ;
20+ import static org .mockito .BDDMockito .*;
21+ import com .ai .lawyer .global .jwt .TokenProvider ;
2522
2623@ Import (SecurityConfig .class )
2724@ AutoConfigureMockMvc
3734class PollControllerTest {
3835 @ Autowired
3936 private MockMvc mockMvc ;
40- @ MockBean
37+ @ MockitoBean
4138 private PollService pollService ;
42- @ MockBean
39+ @ MockitoBean
4340 private com .ai .lawyer .domain .post .service .PostService postService ;
44- @ Autowired
45- private ObjectMapper objectMapper ;
46- @ MockBean
41+ @ MockitoBean
4742 private com .ai .lawyer .global .jwt .TokenProvider tokenProvider ;
48- @ MockBean
43+ @ MockitoBean
4944 private com .ai .lawyer .global .jwt .CookieUtil cookieUtil ;
50- @ MockBean
45+ @ MockitoBean
5146 private com .ai .lawyer .domain .member .repositories .MemberRepository memberRepository ;
52- @ MockBean
47+ @ MockitoBean
5348 private org .springframework .data .jpa .mapping .JpaMetamodelMappingContext jpaMappingContext ;
5449
50+ @ BeforeEach
51+ void setUp () {
52+ // JWT 필터 모킹 설정 - 쿠키에서 토큰 추출 및 검증
53+ given (cookieUtil .getAccessTokenFromCookies (any ())).willReturn ("valid-access-token" );
54+ given (tokenProvider .validateTokenWithResult ("valid-access-token" ))
55+ .willReturn (TokenProvider .TokenValidationResult .VALID );
56+ given (tokenProvider .getMemberIdFromToken ("valid-access-token" )).willReturn (1L );
57+ given (tokenProvider .getRoleFromToken ("valid-access-token" )).willReturn ("USER" );
58+ }
59+
5560 @ Test
5661 @ DisplayName ("투표 단일 조회" )
57- @ WithMockUser (username ="1" )
5862 void t1 () throws Exception {
5963 Mockito .when (pollService .getPoll (Mockito .anyLong ())).thenReturn (null );
60- mockMvc .perform (get ("/api/polls/1" ))
64+
65+ mockMvc .perform (get ("/api/polls/1" )
66+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
6167 .andExpect (status ().isOk ());
6268 }
6369
6470 @ Test
6571 @ DisplayName ("투표 옵션 목록 조회" )
66- @ WithMockUser (username ="1" )
6772 void t2 () throws Exception {
6873 Mockito .when (pollService .getPollOptions (Mockito .anyLong ())).thenReturn (java .util .Collections .emptyList ());
69- mockMvc .perform (get ("/api/polls/1/options" ))
74+
75+ mockMvc .perform (get ("/api/polls/1/options" )
76+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
7077 .andExpect (status ().isOk ());
7178 }
7279
7380 @ Test
7481 @ DisplayName ("투표하기" )
75- @ WithMockUser (username ="1" )
7682 void t3 () throws Exception {
7783 Mockito .when (pollService .vote (Mockito .anyLong (), Mockito .anyLong (), Mockito .anyLong ())).thenReturn (null );
84+
7885 mockMvc .perform (
7986 org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ("/api/polls/1/vote" )
8087 .param ("pollItemsId" , "1" )
88+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
8189 ).andExpect (status ().isOk ());
8290 }
8391
8492 @ Test
8593 @ DisplayName ("투표 통계 조회" )
86- @ WithMockUser (username ="1" )
8794 void t4 () throws Exception {
8895 Mockito .when (pollService .getPollStatics (Mockito .anyLong ())).thenReturn (java .util .Collections .emptyList ());
89- mockMvc .perform (get ("/api/polls/1/statics" ))
96+
97+ mockMvc .perform (get ("/api/polls/1/statics" )
98+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
9099 .andExpect (status ().isOk ());
91100 }
92101
93102 @ Test
94103 @ DisplayName ("투표 종료" )
95- @ WithMockUser (username ="1" )
96104 void t5 () throws Exception {
97105 Mockito .doNothing ().when (pollService ).closePoll (Mockito .anyLong ());
106+
98107 mockMvc .perform (
99108 org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ("/api/polls/1/close" )
109+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
100110 ).andExpect (status ().isOk ());
101111 }
102112
103113 @ Test
104114 @ DisplayName ("투표 삭제" )
105- @ WithMockUser (username ="1" )
106115 void t6 () throws Exception {
107116 Mockito .doNothing ().when (pollService ).deletePoll (Mockito .anyLong ());
117+
108118 mockMvc .perform (
109119 org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ("/api/polls/1" )
120+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
110121 ).andExpect (status ().isOk ());
111122 }
112123
113124 @ Test
114125 @ DisplayName ("진행중인 투표 Top 1 조회" )
115- @ WithMockUser (username ="1" )
116126 void t7 () throws Exception {
117127 Mockito .when (pollService .getTopPollByStatus (Mockito .any ())).thenReturn (null );
118- mockMvc .perform (get ("/api/polls/top/ongoing" ))
128+
129+ mockMvc .perform (get ("/api/polls/top/ongoing" )
130+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
119131 .andExpect (status ().isOk ());
120132 }
121133
122134 @ Test
123135 @ DisplayName ("종료된 투표 Top 1 조회" )
124- @ WithMockUser (username ="1" )
125136 void t8 () throws Exception {
126137 Mockito .when (pollService .getTopPollByStatus (Mockito .any ())).thenReturn (null );
127- mockMvc .perform (get ("/api/polls/top/closed" ))
138+
139+ mockMvc .perform (get ("/api/polls/top/closed" )
140+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
128141 .andExpect (status ().isOk ());
129142 }
130143
131144 @ Test
132145 @ DisplayName ("투표 생성" )
133- @ WithMockUser (username ="1" )
134146 void t9 () throws Exception {
135147 Mockito .when (pollService .createPoll (Mockito .any (), Mockito .anyLong ())).thenReturn (null );
148+
136149 mockMvc .perform (
137150 org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ("/api/polls" )
138151 .contentType (org .springframework .http .MediaType .APPLICATION_JSON )
139152 .content ("{}" )
153+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
140154 ).andExpect (status ().isOk ());
141155 }
142156
143157 @ Test
144158 @ DisplayName ("투표 단일 조회" )
145- @ WithMockUser (username ="1" )
146159 void t10 () throws Exception {
147160 PollDto responseDto = PollDto .builder ().pollId (1L ).voteTitle ("테스트 투표" ).build ();
148161 Mockito .when (pollService .getPoll (Mockito .anyLong ())).thenReturn (responseDto );
149- mockMvc .perform (get ("/api/polls/1" ))
162+
163+ mockMvc .perform (get ("/api/polls/1" )
164+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
150165 .andExpect (status ().isOk ())
151166 .andExpect (org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
152167 .andExpect (org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ("$.result.voteTitle" ).value ("테스트 투표" ));
153168 }
154169
155170 @ Test
156171 @ DisplayName ("투표하기" )
157- @ WithMockUser (username ="1" )
158172 void t11 () throws Exception {
159173 PollVoteDto responseDto = PollVoteDto .builder ().pollId (1L ).memberId (1L ).build ();
160174 Mockito .when (pollService .vote (Mockito .anyLong (), Mockito .anyLong (), Mockito .anyLong ())).thenReturn (responseDto );
175+
161176 mockMvc .perform (
162177 org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ("/api/polls/1/vote" )
163178 .param ("pollItemsId" , "1" )
179+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
164180 ).andExpect (status ().isOk ())
165181 .andExpect (org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
166182 .andExpect (org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ("$.result.memberId" ).value (1L ));
167183 }
168- }
184+ }
0 commit comments