11package com .ai .lawyer .domain .poll .controller ;
22
33import com .ai .lawyer .domain .poll .service .PollService ;
4- import org .junit .jupiter .api .Test ;
4+ import com .ai .lawyer .domain .poll .dto .PollDto ;
5+ import com .ai .lawyer .domain .poll .dto .PollVoteDto ;
6+ import com .ai .lawyer .domain .poll .dto .PollStaticsResponseDto ;
7+ import com .ai .lawyer .global .jwt .TokenProvider ;
8+ import com .ai .lawyer .global .security .SecurityConfig ;
9+ import com .ai .lawyer .domain .post .service .PostService ;
10+ import com .ai .lawyer .domain .member .repositories .MemberRepository ;
11+ import com .ai .lawyer .global .jwt .CookieUtil ;
12+ import com .ai .lawyer .global .oauth .CustomOAuth2UserService ;
13+ import com .ai .lawyer .global .oauth .OAuth2SuccessHandler ;
14+ import com .ai .lawyer .global .oauth .OAuth2FailureHandler ;
515import org .junit .jupiter .api .BeforeEach ;
16+ import org .junit .jupiter .api .DisplayName ;
17+ import org .junit .jupiter .api .Test ;
618import org .mockito .Mockito ;
719import org .springframework .beans .factory .annotation .Autowired ;
820import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
21+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
22+ import org .springframework .context .annotation .Import ;
23+ import org .springframework .http .MediaType ;
924import org .springframework .test .context .bean .override .mockito .MockitoBean ;
1025import org .springframework .test .web .servlet .MockMvc ;
11- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
12- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
13- import com .ai .lawyer .domain .poll .dto .PollDto ;
14- import com .ai .lawyer .domain .poll .dto .PollVoteDto ;
15- import org .springframework .context .annotation .Import ;
16- import com .ai .lawyer .global .security .SecurityConfig ;
17- import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
18- import org .junit .jupiter .api .DisplayName ;
26+ import org .springframework .test .web .servlet .result .MockMvcResultMatchers ;
27+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
28+ import org .springframework .data .jpa .mapping .JpaMetamodelMappingContext ;
29+ import org .springframework .data .redis .core .RedisTemplate ;
1930import jakarta .servlet .http .Cookie ;
31+
2032import static org .mockito .BDDMockito .*;
21- import com .ai .lawyer .global .jwt .TokenProvider ;
22- import com .ai .lawyer .domain .poll .dto .PollStaticsResponseDto ;
33+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
34+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
35+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
36+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
37+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
38+ import org .springframework .boot .autoconfigure .orm .jpa .HibernateJpaAutoConfiguration ;
39+ import org .springframework .boot .autoconfigure .data .jpa .JpaRepositoriesAutoConfiguration ;
40+ import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
41+ import org .springframework .boot .autoconfigure .orm .jpa .JpaBaseConfiguration ;
2342
2443@ Import (SecurityConfig .class )
2544@ AutoConfigureMockMvc
2645@ WebMvcTest (
27- controllers = PollController .class ,
28- excludeAutoConfiguration = {
29- org . springframework . boot . autoconfigure . orm . jpa . HibernateJpaAutoConfiguration .class ,
30- org . springframework . boot . autoconfigure . data . jpa . JpaRepositoriesAutoConfiguration .class ,
31- org . springframework . boot . autoconfigure . jdbc . DataSourceAutoConfiguration .class ,
32- org . springframework . boot . autoconfigure . orm . jpa . JpaBaseConfiguration .class
33- }
46+ controllers = PollController .class ,
47+ excludeAutoConfiguration = {
48+ HibernateJpaAutoConfiguration .class ,
49+ JpaRepositoriesAutoConfiguration .class ,
50+ DataSourceAutoConfiguration .class ,
51+ JpaBaseConfiguration .class
52+ }
3453)
3554class PollControllerTest {
3655 @ Autowired
3756 private MockMvc mockMvc ;
3857 @ MockitoBean
3958 private PollService pollService ;
4059 @ MockitoBean
41- private com . ai . lawyer . domain . post . service . PostService postService ;
60+ private PostService postService ;
4261 @ MockitoBean
43- private com . ai . lawyer . global . jwt . TokenProvider tokenProvider ;
62+ private TokenProvider tokenProvider ;
4463 @ MockitoBean
45- private com . ai . lawyer . global . jwt . CookieUtil cookieUtil ;
64+ private CookieUtil cookieUtil ;
4665 @ MockitoBean
47- private com . ai . lawyer . domain . member . repositories . MemberRepository memberRepository ;
66+ private MemberRepository memberRepository ;
4867 @ MockitoBean
49- private org . springframework . data . jpa . mapping . JpaMetamodelMappingContext jpaMappingContext ;
68+ private JpaMetamodelMappingContext jpaMappingContext ;
5069 @ MockitoBean
51- private org . springframework . data . redis . core . RedisTemplate <String , Object > redisTemplate ;
70+ private RedisTemplate <String , Object > redisTemplate ;
5271 @ MockitoBean
53- private com . ai . lawyer . global . oauth . CustomOAuth2UserService customOAuth2UserService ;
72+ private CustomOAuth2UserService customOAuth2UserService ;
5473 @ MockitoBean
55- private com . ai . lawyer . global . oauth . OAuth2SuccessHandler oauth2SuccessHandler ;
74+ private OAuth2SuccessHandler oauth2SuccessHandler ;
5675 @ MockitoBean
57- private com . ai . lawyer . global . oauth . OAuth2FailureHandler oauth2FailureHandler ;
76+ private OAuth2FailureHandler oauth2FailureHandler ;
5877
5978 @ BeforeEach
6079 void setUp () {
@@ -72,7 +91,7 @@ void t1() throws Exception {
7291 Mockito .when (pollService .getPoll (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (null );
7392
7493 mockMvc .perform (get ("/api/polls/1" )
75- .cookie (new Cookie ("accessToken" , "valid-access-token" )))
94+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
7695 .andExpect (status ().isOk ());
7796 }
7897
@@ -82,7 +101,7 @@ void t2() throws Exception {
82101 Mockito .when (pollService .vote (Mockito .anyLong (), Mockito .anyLong (), Mockito .anyLong ())).thenReturn (null );
83102
84103 mockMvc .perform (
85- org . springframework . test . web . servlet . request . MockMvcRequestBuilders . post ("/api/polls/1/vote" )
104+ post ("/api/polls/1/vote" )
86105 .param ("pollItemsId" , "1" )
87106 .cookie (new Cookie ("accessToken" , "valid-access-token" ))
88107 ).andExpect (status ().isOk ());
@@ -94,7 +113,7 @@ void t3() throws Exception {
94113 Mockito .when (pollService .getPollStatics (Mockito .anyLong ())).thenReturn (new PollStaticsResponseDto ());
95114
96115 mockMvc .perform (get ("/api/polls/1/statics" )
97- .cookie (new Cookie ("accessToken" , "valid-access-token" )))
116+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
98117 .andExpect (status ().isOk ());
99118 }
100119
@@ -104,7 +123,7 @@ void t4() throws Exception {
104123 Mockito .doNothing ().when (pollService ).closePoll (Mockito .anyLong ());
105124
106125 mockMvc .perform (
107- org . springframework . test . web . servlet . request . MockMvcRequestBuilders . put ("/api/polls/1/close" )
126+ put ("/api/polls/1/close" )
108127 .cookie (new Cookie ("accessToken" , "valid-access-token" ))
109128 ).andExpect (status ().isOk ());
110129 }
@@ -117,7 +136,7 @@ void t5() throws Exception {
117136 Mockito .doNothing ().when (pollService ).deletePoll (Mockito .anyLong (), Mockito .anyLong ());
118137
119138 mockMvc .perform (
120- org . springframework . test . web . servlet . request . MockMvcRequestBuilders . delete ("/api/polls/1" )
139+ delete ("/api/polls/1" )
121140 .cookie (new Cookie ("accessToken" , "valid-access-token" ))
122141 ).andExpect (status ().isOk ());
123142 }
@@ -128,7 +147,7 @@ void t6() throws Exception {
128147 Mockito .when (pollService .getTopPollByStatus (Mockito .any (), Mockito .anyLong ())).thenReturn (null );
129148
130149 mockMvc .perform (get ("/api/polls/top/ongoing" )
131- .cookie (new Cookie ("accessToken" , "valid-access-token" )))
150+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
132151 .andExpect (status ().isOk ());
133152 }
134153
@@ -138,7 +157,7 @@ void t7() throws Exception {
138157 Mockito .when (pollService .getTopPollByStatus (Mockito .any (), Mockito .anyLong ())).thenReturn (null );
139158
140159 mockMvc .perform (get ("/api/polls/top/closed" )
141- .cookie (new Cookie ("accessToken" , "valid-access-token" )))
160+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
142161 .andExpect (status ().isOk ());
143162 }
144163
@@ -148,8 +167,8 @@ void t8() throws Exception {
148167 Mockito .when (pollService .createPoll (Mockito .any (), Mockito .anyLong ())).thenReturn (null );
149168
150169 mockMvc .perform (
151- org . springframework . test . web . servlet . request . MockMvcRequestBuilders . post ("/api/polls" )
152- .contentType (org . springframework . http . MediaType .APPLICATION_JSON )
170+ post ("/api/polls" )
171+ .contentType (MediaType .APPLICATION_JSON )
153172 .content ("{}" )
154173 .cookie (new Cookie ("accessToken" , "valid-access-token" ))
155174 ).andExpect (status ().isOk ());
@@ -162,10 +181,10 @@ void t9() throws Exception {
162181 Mockito .when (pollService .getPoll (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (responseDto );
163182
164183 mockMvc .perform (get ("/api/polls/1" )
165- .cookie (new Cookie ("accessToken" , "valid-access-token" )))
184+ .cookie (new Cookie ("accessToken" , "valid-access-token" )))
166185 .andExpect (status ().isOk ())
167- .andExpect (org . springframework . test . web . servlet . result . MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
168- .andExpect (org . springframework . test . web . servlet . result . MockMvcResultMatchers .jsonPath ("$.result.voteTitle" ).value ("테스트 투표" ));
186+ .andExpect (MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
187+ .andExpect (MockMvcResultMatchers .jsonPath ("$.result.voteTitle" ).value ("테스트 투표" ));
169188 }
170189
171190 @ Test
@@ -175,11 +194,11 @@ void t10() throws Exception {
175194 Mockito .when (pollService .vote (Mockito .anyLong (), Mockito .anyLong (), Mockito .anyLong ())).thenReturn (responseDto );
176195
177196 mockMvc .perform (
178- org . springframework . test . web . servlet . request . MockMvcRequestBuilders . post ("/api/polls/1/vote" )
179- .param ("pollItemsId" , "1" )
180- .cookie (new Cookie ("accessToken" , "valid-access-token" ))
181- ).andExpect (status ().isOk ())
182- .andExpect (org . springframework . test . web . servlet . result . MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
183- .andExpect (org . springframework . test . web . servlet . result . MockMvcResultMatchers .jsonPath ("$.result.memberId" ).value (1L ));
197+ post ("/api/polls/1/vote" )
198+ .param ("pollItemsId" , "1" )
199+ .cookie (new Cookie ("accessToken" , "valid-access-token" ))
200+ ).andExpect (status ().isOk ())
201+ .andExpect (MockMvcResultMatchers .jsonPath ("$.result.pollId" ).value (1L ))
202+ .andExpect (MockMvcResultMatchers .jsonPath ("$.result.memberId" ).value (1L ));
184203 }
185204}
0 commit comments