11package com .back .domain .board .controller ;
22
33import com .back .domain .board .dto .PostRequest ;
4+ import com .back .domain .board .entity .Post ;
45import com .back .domain .board .entity .PostCategory ;
56import com .back .domain .board .repository .PostCategoryRepository ;
7+ import com .back .domain .board .repository .PostRepository ;
68import com .back .domain .user .entity .User ;
79import com .back .domain .user .entity .UserProfile ;
810import com .back .domain .user .entity .UserStatus ;
2426import java .time .LocalDate ;
2527import java .util .List ;
2628
27- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
29+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .* ;
2830import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
2931import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
3032
@@ -40,6 +42,9 @@ class PostControllerTest {
4042 @ Autowired
4143 private UserRepository userRepository ;
4244
45+ @ Autowired
46+ private PostRepository postRepository ;
47+
4348 @ Autowired
4449 private PostCategoryRepository postCategoryRepository ;
4550
@@ -168,4 +173,82 @@ void createPost_badRequest() throws Exception {
168173 .andExpect (jsonPath ("$.code" ).value ("COMMON_400" ))
169174 .andExpect (jsonPath ("$.message" ).value ("잘못된 요청입니다." ));
170175 }
176+
177+ // ====================== 게시글 조회 테스트 ======================
178+
179+ @ Test
180+ @ DisplayName ("게시글 다건 조회 성공 → 200 OK" )
181+ void getPosts_success () throws Exception {
182+ // given: 유저 + 카테고리 + 게시글 2개
183+ User user =
User .
createUser (
"reader" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
184+ user .setUserProfile (new UserProfile (user , "홍길동" , null , null , null , 0 ));
185+ user .setUserStatus (UserStatus .ACTIVE );
186+ userRepository .save (user );
187+
188+ PostCategory c1 = new PostCategory ("공지사항" );
189+ PostCategory c2 = new PostCategory ("자유게시판" );
190+ postCategoryRepository .saveAll (List .of (c1 , c2 ));
191+
192+ Post post1 = new Post (user , "첫 글" , "내용1" );
193+ post1 .updateCategories (List .of (c1 ));
194+ postRepository .save (post1 );
195+
196+ Post post2 = new Post (user , "두 번째 글" , "내용2" );
197+ post2 .updateCategories (List .of (c2 ));
198+ postRepository .save (post2 );
199+
200+ // when
201+ mvc .perform (get ("/api/posts" )
202+ .param ("page" , "0" )
203+ .param ("size" , "10" )
204+ .contentType (MediaType .APPLICATION_JSON ))
205+ .andDo (print ())
206+ // then
207+ .andExpect (status ().isOk ())
208+ .andExpect (jsonPath ("$.success" ).value (true ))
209+ .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
210+ .andExpect (jsonPath ("$.data.items.length()" ).value (2 ))
211+ .andExpect (jsonPath ("$.data.items[0].author.nickname" ).value ("홍길동" ));
212+ }
213+
214+ @ Test
215+ @ DisplayName ("게시글 단건 조회 성공 → 200 OK" )
216+ void getPost_success () throws Exception {
217+ // given
218+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
219+ user .setUserProfile (new UserProfile (user , "이몽룡" , null , null , null , 0 ));
220+ user .setUserStatus (UserStatus .ACTIVE );
221+ userRepository .save (user );
222+
223+ PostCategory c1 = new PostCategory ("공지사항" );
224+ postCategoryRepository .save (c1 );
225+
226+ Post post = new Post (user , "조회 테스트 글" , "조회 테스트 내용" );
227+ post .updateCategories (List .of (c1 ));
228+ postRepository .save (post );
229+
230+ // when
231+ mvc .perform (get ("/api/posts/{postId}" , post .getId ())
232+ .contentType (MediaType .APPLICATION_JSON ))
233+ .andDo (print ())
234+ // then
235+ .andExpect (status ().isOk ())
236+ .andExpect (jsonPath ("$.success" ).value (true ))
237+ .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
238+ .andExpect (jsonPath ("$.data.postId" ).value (post .getId ()))
239+ .andExpect (jsonPath ("$.data.title" ).value ("조회 테스트 글" ))
240+ .andExpect (jsonPath ("$.data.author.nickname" ).value ("이몽룡" ))
241+ .andExpect (jsonPath ("$.data.categories[0].name" ).value ("공지사항" ));
242+ }
243+
244+ @ Test
245+ @ DisplayName ("게시글 단건 조회 실패 - 존재하지 않는 게시글 → 404 Not Found" )
246+ void getPost_fail_notFound () throws Exception {
247+ mvc .perform (get ("/api/posts/{postId}" , 999L )
248+ .contentType (MediaType .APPLICATION_JSON ))
249+ .andDo (print ())
250+ .andExpect (status ().isNotFound ())
251+ .andExpect (jsonPath ("$.code" ).value ("POST_001" ))
252+ .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 게시글입니다." ));
253+ }
171254}
0 commit comments