1414import org .mockito .Mock ;
1515import org .mockito .junit .jupiter .MockitoExtension ;
1616
17+ import org .springframework .security .access .AccessDeniedException ;
1718import java .util .Arrays ;
1819import java .util .List ;
20+ import java .util .NoSuchElementException ;
1921import java .util .Optional ;
2022
2123import static org .assertj .core .api .Assertions .assertThat ;
2426import static org .mockito .Mockito .*;
2527
2628@ ExtendWith (MockitoExtension .class )
27- class PostCommentServiceTest {
29+ class NewsCommentServiceTest {
2830
2931 @ Mock
3032 private CommentRepository commentRepository ;
@@ -110,11 +112,12 @@ void updateComment_CommentNotFound_ThrowsException() {
110112 when (commentRepository .findById (nonExistentCommentId )).thenReturn (Optional .empty ());
111113
112114 // when & then
113- IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
115+ NoSuchElementException exception = assertThrows (NoSuchElementException .class , () -> {
114116 commentService .updateComment (member , news , nonExistentCommentId , updatedContent );
115117 });
116118
117- assertThat (exception .getMessage ()).isEqualTo ("Comment not found" );
119+
120+ assertThat (exception .getMessage ()).isEqualTo ("Comment not found: " + nonExistentCommentId );
118121 verify (commentRepository , times (1 )).findById (nonExistentCommentId );
119122 }
120123
@@ -132,7 +135,7 @@ void updateComment_NotAuthor_ThrowsException() {
132135 when (commentRepository .findById (commentId )).thenReturn (Optional .of (existingComment ));
133136
134137 // when & then
135- IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
138+ AccessDeniedException exception = assertThrows (AccessDeniedException .class , () -> {
136139 commentService .updateComment (otherMember , news , commentId , updatedContent );
137140 });
138141
@@ -193,11 +196,11 @@ void deleteComment_CommentNotFound_ThrowsException() {
193196 when (commentRepository .findById (nonExistentCommentId )).thenReturn (Optional .empty ());
194197
195198 // when & then
196- IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
199+ NoSuchElementException exception = assertThrows (NoSuchElementException .class , () -> {
197200 commentService .deleteComment (member , news , nonExistentCommentId );
198201 });
199202
200- assertThat (exception .getMessage ()).isEqualTo ("Comment not found" );
203+ assertThat (exception .getMessage ()).isEqualTo ("Comment not found: " + nonExistentCommentId );
201204 verify (commentRepository , times (1 )).findById (nonExistentCommentId );
202205 verify (commentRepository , never ()).delete (any (Comment .class ));
203206 }
@@ -215,7 +218,7 @@ void deleteComment_NotAuthor_ThrowsException() {
215218 when (commentRepository .findById (commentId )).thenReturn (Optional .of (existingComment ));
216219
217220 // when & then
218- IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
221+ AccessDeniedException exception = assertThrows (AccessDeniedException .class , () -> {
219222 commentService .deleteComment (otherMember , news , commentId );
220223 });
221224
0 commit comments