Skip to content

Commit b7b0d9d

Browse files
committed
test(community): CommunityComment 생성 시 게시글 검증 테스트 추가
1 parent 0f63ec6 commit b7b0d9d

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/test/java/com/somemore/community/service/comment/CreateCommunityCommentServiceTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import com.somemore.IntegrationTestSupport;
44
import com.somemore.community.domain.CommunityComment;
5+
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
56
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
67
import com.somemore.community.repository.comment.CommunityCommentRepository;
8+
import com.somemore.community.usecase.board.CreateCommunityBoardUseCase;
9+
import com.somemore.community.usecase.board.DeleteCommunityBoardUseCase;
710
import com.somemore.global.exception.BadRequestException;
811
import com.somemore.global.exception.ExceptionMessage;
912
import org.assertj.core.api.ThrowableAssert;
1013
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
1115
import org.junit.jupiter.api.DisplayName;
1216
import org.junit.jupiter.api.Test;
1317
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +28,25 @@ class CreateCommunityCommentServiceTest extends IntegrationTestSupport {
2428
private CreateCommunityCommentService createCommunityCommentService;
2529
@Autowired
2630
private CommunityCommentRepository communityCommentRepository;
31+
@Autowired
32+
private CreateCommunityBoardUseCase createCommunityBoardUseCase;
33+
@Autowired
34+
private DeleteCommunityBoardUseCase deleteCommunityBoardUseCase;
35+
36+
private UUID writerId;
37+
private Long boardId;
38+
39+
@BeforeEach
40+
void setUp() {
41+
CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder()
42+
.title("커뮤니티 테스트 제목")
43+
.content("커뮤니티 테스트 내용")
44+
.build();
45+
46+
writerId = UUID.randomUUID();
47+
48+
boardId = createCommunityBoardUseCase.createCommunityBoard(boardDto, writerId, null);
49+
}
2750

2851
@AfterEach
2952
void tearDown() {
@@ -36,6 +59,7 @@ void createCommunityCommentWithDto() {
3659

3760
//given
3861
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
62+
.communityBoardId(boardId)
3963
.content("커뮤니티 댓글 테스트 내용")
4064
.parentCommentId(null)
4165
.build();
@@ -61,6 +85,7 @@ void createCommunityCommentRelyWithDto() {
6185

6286
//given
6387
CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder()
88+
.communityBoardId(boardId)
6489
.content("커뮤니티 댓글 테스트 내용")
6590
.parentCommentId(null)
6691
.build();
@@ -69,6 +94,7 @@ void createCommunityCommentRelyWithDto() {
6994
Long commentId = createCommunityCommentService.createCommunityComment(commentDto, writerId);
7095

7196
CommunityCommentCreateRequestDto replyDto = CommunityCommentCreateRequestDto.builder()
97+
.communityBoardId(boardId)
7298
.content("커뮤니티 대댓글 테스트 내용")
7399
.parentCommentId(commentId)
74100
.build();
@@ -86,12 +112,13 @@ void createCommunityCommentRelyWithDto() {
86112
assertThat(communityCommentReply.get().getParentCommentId()).isEqualTo(commentId);
87113
}
88114

89-
@DisplayName("삭제된 댓글에 대댓글을 등록할 때 예외를 던진다.")
115+
@DisplayName("삭제된 댓글이나 존재하지 않는 댓글에 대댓글을 등록할 때 예외를 던진다.")
90116
@Test
91117
void createCommunityCommentReplyWithDeletedParentId() {
92118

93119
//given
94120
CommunityCommentCreateRequestDto replyDto = CommunityCommentCreateRequestDto.builder()
121+
.communityBoardId(boardId)
95122
.content("커뮤니티 대댓글 테스트 내용")
96123
.parentCommentId(2L)
97124
.build();
@@ -104,4 +131,26 @@ void createCommunityCommentReplyWithDeletedParentId() {
104131
.isThrownBy(callable)
105132
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT.getMessage());
106133
}
134+
135+
@DisplayName("삭제된 게시글에 댓글을 등록할 때 예외를 던진다.")
136+
@Test
137+
void createCommunityCommentWithDeletedBoardId() {
138+
139+
//given
140+
CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder()
141+
.communityBoardId(boardId)
142+
.content("커뮤니티 댓글 테스트 내용")
143+
.parentCommentId(null)
144+
.build();
145+
146+
deleteCommunityBoardUseCase.deleteCommunityBoard(writerId, boardId);
147+
148+
//when
149+
ThrowableAssert.ThrowingCallable callable = () -> createCommunityCommentService.createCommunityComment(commentDto, UUID.randomUUID());
150+
151+
//then
152+
assertThatExceptionOfType(BadRequestException.class)
153+
.isThrownBy(callable)
154+
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage());
155+
}
107156
}

src/test/java/com/somemore/community/service/comment/DeleteCommunityCommentServiceTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.somemore.community.service.comment;
22

33
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
45
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
56
import com.somemore.community.repository.comment.CommunityCommentRepository;
7+
import com.somemore.community.usecase.board.CreateCommunityBoardUseCase;
68
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
79
import com.somemore.global.exception.BadRequestException;
810
import com.somemore.global.exception.ExceptionMessage;
@@ -26,13 +28,25 @@ class DeleteCommunityCommentServiceTest extends IntegrationTestSupport {
2628
private CreateCommunityCommentUseCase createCommunityCommentUseCase;
2729
@Autowired
2830
private CommunityCommentRepository communityCommentRepository;
31+
@Autowired
32+
private CreateCommunityBoardUseCase createCommunityBoardUseCase;
2933

3034
private UUID writerId;
3135
private Long commentId;
3236

3337
@BeforeEach
3438
void setUp() {
39+
CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder()
40+
.title("커뮤니티 테스트 제목")
41+
.content("커뮤니티 테스트 내용")
42+
.build();
43+
44+
writerId = UUID.randomUUID();
45+
46+
Long boardId = createCommunityBoardUseCase.createCommunityBoard(boardDto, writerId, null);
47+
3548
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
49+
.communityBoardId(boardId)
3650
.content("커뮤니티 댓글 테스트 내용")
3751
.parentCommentId(null)
3852
.build();

0 commit comments

Comments
 (0)