Skip to content

Commit 6094c71

Browse files
committed
test(community): communityComment 생성 기능 테스트 작성
1 parent 49bff8b commit 6094c71

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.somemore.community.repository;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.community.domain.CommunityComment;
5+
import com.somemore.community.repository.comment.CommunityCommentRepository;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.transaction.annotation.Transactional;
10+
import java.util.UUID;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@Transactional
15+
public class CommunityCommentRepositoryTest extends IntegrationTestSupport {
16+
@Autowired
17+
CommunityCommentRepository communityCommentRepository;
18+
19+
@DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)")
20+
@Test
21+
void createCommunityComment() {
22+
23+
//given
24+
UUID writerId = UUID.randomUUID();
25+
26+
CommunityComment communityComment = CommunityComment.builder()
27+
.writerId(writerId)
28+
.content("커뮤니티 댓글 테스트 내용")
29+
.parentCommentId(null)
30+
.build();
31+
32+
//when
33+
CommunityComment savedComment = communityCommentRepository.save(communityComment);
34+
35+
//then
36+
assertThat(savedComment.getWriterId()).isEqualTo(writerId);
37+
assertThat(savedComment.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
38+
assertThat(savedComment.getParentCommentId()).isNull();
39+
}
40+
41+
@DisplayName("커뮤니티 게시글에 대댓글을 생성할 수 있다. (Repository)")
42+
@Test
43+
void createCommunityCommentReply() {
44+
45+
//given
46+
UUID writerId = UUID.randomUUID();
47+
48+
CommunityComment communityComment = CommunityComment.builder()
49+
.writerId(writerId)
50+
.content("커뮤니티 댓글 테스트 내용")
51+
.parentCommentId(1L)
52+
.build();
53+
54+
//when
55+
CommunityComment savedComment = communityCommentRepository.save(communityComment);
56+
57+
//then
58+
assertThat(savedComment.getWriterId()).isEqualTo(writerId);
59+
assertThat(savedComment.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
60+
assertThat(savedComment.getParentCommentId()).isEqualTo(1L);
61+
}
62+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.somemore.community.service.comment;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.community.domain.CommunityComment;
5+
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
6+
import com.somemore.community.repository.comment.CommunityCommentRepository;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
12+
import java.util.Optional;
13+
import java.util.UUID;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
public class CreateCommunityCommentServiceTest extends IntegrationTestSupport {
18+
19+
@Autowired
20+
private CreateCommunityCommentService createCommunityCommentService;
21+
@Autowired
22+
private CommunityCommentRepository communityCommentRepository;
23+
24+
@AfterEach
25+
void tearDown() {
26+
communityCommentRepository.deleteAllInBatch();
27+
}
28+
29+
@DisplayName("커뮤니티 게시글에 댓글을 등록한다.")
30+
@Test
31+
void createCommunityCommentWithDto() {
32+
33+
//given
34+
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
35+
.content("커뮤니티 댓글 테스트 내용")
36+
.build();
37+
38+
UUID writerId = UUID.randomUUID();
39+
40+
//when
41+
Long commentId = createCommunityCommentService.CreateCommunityComment(dto, writerId, null);
42+
43+
//then
44+
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
45+
46+
assertThat(communityComment).isPresent();
47+
assertThat(communityComment.get().getId()).isEqualTo(commentId);
48+
assertThat(communityComment.get().getWriterId()).isEqualTo(writerId);
49+
assertThat(communityComment.get().getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
50+
assertThat(communityComment.get().getParentCommentId()).isNull();
51+
}
52+
53+
@DisplayName("커뮤니티 게시글에 대댓글을 등록한다.")
54+
@Test
55+
void createCommunityCommenReplytWithDto() {
56+
57+
//given
58+
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
59+
.content("커뮤니티 댓글 테스트 내용")
60+
.build();
61+
62+
UUID writerId = UUID.randomUUID();
63+
64+
//when
65+
Long commentId = createCommunityCommentService.CreateCommunityComment(dto, writerId, 2L);
66+
67+
//then
68+
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
69+
70+
assertThat(communityComment).isPresent();
71+
assertThat(communityComment.get().getId()).isEqualTo(commentId);
72+
assertThat(communityComment.get().getWriterId()).isEqualTo(writerId);
73+
assertThat(communityComment.get().getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
74+
assertThat(communityComment.get().getParentCommentId()).isEqualTo(2L);
75+
}
76+
}

0 commit comments

Comments
 (0)