1111import com .rentify .rentify_api .category .exception .CategoryNotFoundException ;
1212import com .rentify .rentify_api .category .repository .CategoryRepository ;
1313import com .rentify .rentify_api .common .exception .IdempotencyException ;
14+ import com .rentify .rentify_api .common .exception .NotFoundException ;
1415import com .rentify .rentify_api .common .idempotency .IdempotencyKey ;
1516import com .rentify .rentify_api .common .idempotency .IdempotencyKeyRepository ;
1617import com .rentify .rentify_api .common .idempotency .IdempotencyStatus ;
18+ import com .rentify .rentify_api .image .entity .Image ;
1719import com .rentify .rentify_api .image .service .ImageService ;
1820import com .rentify .rentify_api .post .dto .CreatePostRequest ;
21+ import com .rentify .rentify_api .post .dto .PostDetailResponse ;
1922import com .rentify .rentify_api .post .entity .Post ;
2023import com .rentify .rentify_api .post .repository .PostHistoryRepository ;
2124import com .rentify .rentify_api .post .repository .PostRepository ;
@@ -206,4 +209,70 @@ void create_post_category_notfound() {
206209 verify (postHistoryRepository , times (0 )).save (any ());
207210 verify (postRepository , times (0 )).save (any ());
208211 }
212+
213+ @ Test
214+ @ DisplayName ("게시글 상세 조회 성공" )
215+ void get_post_success () {
216+ // given
217+ Long postId = 1L ;
218+
219+ User mockUser = User .builder ()
220+ .id (100L )
221+ .name ("테스트유저" )
222+ .build ();
223+
224+ Category mockCategory = Category .builder ()
225+ .id (200L )
226+ .name ("전자기기" )
227+ .build ();
228+
229+ Image mockImage1 = Image .builder ().url ("http://test.com/1.jpg" ).build ();
230+ Image mockImage2 = Image .builder ().url ("http://test.com/2.jpg" ).build ();
231+
232+ Post mockPost = Post .builder ()
233+ .id (postId )
234+ .title ("테스트 제목" )
235+ .description ("테스트 내용" )
236+ .pricePerDay (5000 )
237+ .maxRentalDays (5 )
238+ .isParcel (true )
239+ .images (List .of (mockImage1 , mockImage2 ))
240+ .user (mockUser )
241+ .category (mockCategory )
242+ .build ();
243+
244+ given (postRepository .findById (postId )).willReturn (Optional .of (mockPost ));
245+
246+ // when
247+ PostDetailResponse response = postService .getPost (postId );
248+
249+ // then
250+ assertThat (response .getPostId ()).isEqualTo (postId );
251+ assertThat (response .getTitle ()).isEqualTo ("테스트 제목" );
252+ assertThat (response .getUserId ()).isEqualTo (100L );
253+ assertThat (response .getUserName ()).isEqualTo ("테스트유저" );
254+ assertThat (response .getCategoryName ()).isEqualTo ("전자기기" );
255+ assertThat (response .getImageUrls ()).hasSize (2 );
256+ assertThat (response .getImageUrls ()).containsExactly (
257+ "http://test.com/1.jpg" ,
258+ "http://test.com/2.jpg"
259+ );
260+
261+ // verify
262+ verify (postRepository , times (1 )).findById (any ());
263+ }
264+
265+ @ Test
266+ @ DisplayName ("게시글 상세 조회 실패" )
267+ void get_post_notfound () {
268+ // given
269+ Long invalidPostId = 10L ;
270+
271+ given (postRepository .findById (invalidPostId )).willReturn (Optional .empty ());
272+
273+ // when & then
274+ assertThatThrownBy (() -> postService .getPost (invalidPostId ))
275+ .isInstanceOf (NotFoundException .class )
276+ .hasMessage ("게시글을 찾을 수 없습니다." );
277+ }
209278}
0 commit comments