Skip to content

Commit e4eea22

Browse files
committed
Test: 대댓글 수정/삭제 테스트 작성
1 parent dfbdef2 commit e4eea22

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

src/test/java/com/back/domain/board/controller/CommentControllerTest.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ void deleteComment_noToken() throws Exception {
583583
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
584584
}
585585

586-
// ====================== 대댓글 생성 테스트 ======================
586+
// ====================== 대댓글 테스트 ======================
587587

588588
@Test
589589
@DisplayName("대댓글 생성 성공 → 201 Created")
@@ -825,4 +825,60 @@ void createReply_noToken() throws Exception {
825825
.andExpect(jsonPath("$.code").value("AUTH_001"))
826826
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
827827
}
828+
829+
@Test
830+
@DisplayName("대댓글 수정 성공 → 200 OK")
831+
void updateReply_success() throws Exception {
832+
// given
833+
User user = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
834+
user.setUserProfile(new UserProfile(user, "이몽룡", null, null, null, 0));
835+
user.setUserStatus(UserStatus.ACTIVE);
836+
userRepository.save(user);
837+
838+
Post post = new Post(user, "제목", "내용");
839+
postRepository.save(post);
840+
841+
Comment parent = new Comment(post, user, "부모 댓글", null);
842+
Comment reply = new Comment(post, user, "대댓글", parent);
843+
commentRepository.saveAll(List.of(parent, reply));
844+
845+
String accessToken = generateAccessToken(user);
846+
CommentRequest request = new CommentRequest("수정된 대댓글 내용");
847+
848+
// when & then
849+
mvc.perform(put("/api/posts/{postId}/comments/{commentId}", post.getId(), reply.getId())
850+
.header("Authorization", "Bearer " + accessToken)
851+
.contentType(MediaType.APPLICATION_JSON)
852+
.content(objectMapper.writeValueAsString(request)))
853+
.andDo(print())
854+
.andExpect(status().isOk())
855+
.andExpect(jsonPath("$.data.content").value("수정된 대댓글 내용"));
856+
}
857+
858+
@Test
859+
@DisplayName("대댓글 삭제 성공 → 200 OK")
860+
void deleteReply_success() throws Exception {
861+
// given
862+
User user = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
863+
user.setUserProfile(new UserProfile(user, "이몽룡", null, null, null, 0));
864+
user.setUserStatus(UserStatus.ACTIVE);
865+
userRepository.save(user);
866+
867+
Post post = new Post(user, "제목", "내용");
868+
postRepository.save(post);
869+
870+
Comment parent = new Comment(post, user, "부모 댓글", null);
871+
Comment reply = new Comment(post, user, "대댓글", parent);
872+
commentRepository.saveAll(List.of(parent, reply));
873+
874+
String accessToken = generateAccessToken(user);
875+
876+
// when & then
877+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", post.getId(), reply.getId())
878+
.header("Authorization", "Bearer " + accessToken))
879+
.andDo(print())
880+
.andExpect(status().isOk())
881+
.andExpect(jsonPath("$.success").value(true))
882+
.andExpect(jsonPath("$.message").value("댓글이 삭제되었습니다."));
883+
}
828884
}

src/test/java/com/back/domain/board/service/CommentServiceTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ void deleteComment_fail_noPermission() {
348348
.hasMessage(ErrorCode.COMMENT_NO_PERMISSION.getMessage());
349349
}
350350

351-
// ====================== 대댓글 생성 테스트 ======================
351+
// ====================== 대댓글 테스트 ======================
352352

353353
@Test
354354
@DisplayName("대댓글 생성 성공")
@@ -445,4 +445,55 @@ void createReply_fail_commentNotFound() {
445445
.isInstanceOf(CustomException.class)
446446
.hasMessage(ErrorCode.COMMENT_NOT_FOUND.getMessage());
447447
}
448+
449+
@Test
450+
@DisplayName("대댓글 수정 성공")
451+
void updateReply_success() {
452+
// given
453+
User user = User.createUser("writer", "[email protected]", "encodedPwd");
454+
user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0));
455+
user.setUserStatus(UserStatus.ACTIVE);
456+
userRepository.save(user);
457+
458+
Post post = new Post(user, "제목", "내용");
459+
postRepository.save(post);
460+
461+
Comment parent = new Comment(post, user, "부모 댓글", null);
462+
Comment reply = new Comment(post, user, "대댓글", parent);
463+
commentRepository.saveAll(List.of(parent, reply));
464+
465+
CommentRequest updateRequest = new CommentRequest("수정된 대댓글 내용");
466+
467+
// when
468+
CommentResponse updated = commentService.updateComment(post.getId(), reply.getId(), updateRequest, user.getId());
469+
470+
// then
471+
assertThat(updated.content()).isEqualTo("수정된 대댓글 내용");
472+
assertThat(updated.commentId()).isEqualTo(reply.getId());
473+
assertThat(updated.author().nickname()).isEqualTo("작성자");
474+
}
475+
476+
@Test
477+
@DisplayName("대댓글 삭제 성공")
478+
void deleteReply_success() {
479+
// given
480+
User user = User.createUser("writer", "[email protected]", "encodedPwd");
481+
user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0));
482+
user.setUserStatus(UserStatus.ACTIVE);
483+
userRepository.save(user);
484+
485+
Post post = new Post(user, "제목", "내용");
486+
postRepository.save(post);
487+
488+
Comment parent = new Comment(post, user, "부모 댓글", null);
489+
Comment reply = new Comment(post, user, "삭제할 대댓글", parent);
490+
commentRepository.saveAll(List.of(parent, reply));
491+
492+
// when
493+
commentService.deleteComment(post.getId(), reply.getId(), user.getId());
494+
495+
// then
496+
boolean exists = commentRepository.findById(reply.getId()).isPresent();
497+
assertThat(exists).isFalse();
498+
}
448499
}

0 commit comments

Comments
 (0)