Skip to content

Commit 8abf87b

Browse files
committed
test(community): 수정된 변수명 반영 및 조회 테스트 페이징으로 변경
- DTO to/from 변수명 반영 - 게시글, 댓글 조회 테스트 페이징 처리 반영
1 parent 7d0e75c commit 8abf87b

8 files changed

+153
-265
lines changed

src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java

Lines changed: 56 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
import com.somemore.community.repository.board.CommunityBoardRepository;
88
import com.somemore.volunteer.domain.Volunteer;
99
import com.somemore.volunteer.repository.VolunteerRepository;
10+
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.DisplayName;
1112
import org.junit.jupiter.api.Test;
1213
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.data.domain.Page;
15+
import org.springframework.data.domain.PageRequest;
16+
import org.springframework.data.domain.Pageable;
1317
import org.springframework.transaction.annotation.Transactional;
1418

15-
import java.util.List;
19+
import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard;
20+
1621
import java.util.Optional;
1722
import java.util.UUID;
1823

@@ -26,17 +31,37 @@ class CommunityBoardRepositoryTest extends IntegrationTestSupport {
2631
@Autowired
2732
private VolunteerRepository volunteerRepository;
2833

34+
private UUID writerId;
35+
36+
@BeforeEach
37+
void setUp() {
38+
String oAuthId1 = "example-oauth-id1";
39+
Volunteer volunteer1 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId1);
40+
volunteerRepository.save(volunteer1);
41+
writerId = volunteer1.getId();
42+
43+
String oAuthId2 = "example-oauth-id2";
44+
Volunteer volunteer2 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId2);
45+
volunteerRepository.save(volunteer2);
46+
47+
for (int i = 1; i <= 8; i++) {
48+
String title = "제목" + i;
49+
CommunityBoard communityBoard = createCommunityBoard(title, writerId);
50+
communityBoardRepository.save(communityBoard);
51+
}
52+
53+
for (int i = 1; i <= 20; i++) {
54+
String title = "제목" + i;
55+
CommunityBoard communityBoard = createCommunityBoard(title, volunteer2.getId());
56+
communityBoardRepository.save(communityBoard);
57+
}
58+
}
59+
2960
@DisplayName("커뮤니티 게시글 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)")
3061
@Test
3162
void getCommunityBoardById() {
3263
//given
33-
CommunityBoard communityBoard = CommunityBoard.builder()
34-
.title("테스트 커뮤니티 게시글 제목")
35-
.content("테스트 커뮤니티 게시글 내용")
36-
.imgUrl("http://community.example.com/123")
37-
.writerId(UUID.randomUUID())
38-
.build();
39-
64+
CommunityBoard communityBoard = createCommunityBoard(UUID.randomUUID());
4065
communityBoardRepository.save(communityBoard);
4166

4267
//when
@@ -54,14 +79,9 @@ void getCommunityBoardById() {
5479
@Test
5580
void getCommunityBoardByDeletedId() {
5681
//given
57-
CommunityBoard communityBoard = CommunityBoard.builder()
58-
.title("테스트 커뮤니티 게시글 제목")
59-
.content("테스트 커뮤니티 게시글 내용")
60-
.imgUrl("http://community.example.com/123")
61-
.writerId(UUID.randomUUID())
62-
.build();
63-
82+
CommunityBoard communityBoard = createCommunityBoard(UUID.randomUUID());
6483
communityBoardRepository.save(communityBoard);
84+
6585
communityBoardRepository.deleteAllInBatch();
6686

6787
//when
@@ -71,114 +91,54 @@ void getCommunityBoardByDeletedId() {
7191
assertThat(foundCommunityBoard).isEmpty();
7292
}
7393

74-
@DisplayName("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)")
94+
@DisplayName("저장된 전체 커뮤니티 게시글을 페이지로 조회할 수 있다. (Repository)")
7595
@Test
7696
void getCommunityBoards() {
97+
7798
//given
78-
String oAuthId = "example-oauth-id";
79-
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
80-
volunteerRepository.save(volunteer);
81-
82-
CommunityBoard communityBoard1 = CommunityBoard.builder()
83-
.title("테스트 커뮤니티 게시글 제목1")
84-
.content("테스트 커뮤니티 게시글 내용1")
85-
.imgUrl("http://community.example.com/123")
86-
.writerId(volunteer.getId())
87-
.build();
88-
89-
CommunityBoard communityBoard2 = CommunityBoard.builder()
90-
.title("테스트 커뮤니티 게시글 제목2")
91-
.content("테스트 커뮤니티 게시글 내용2")
92-
.imgUrl("http://community.example.com/12")
93-
.writerId(volunteer.getId())
94-
.build();
95-
96-
communityBoardRepository.save(communityBoard1);
97-
communityBoardRepository.save(communityBoard2);
99+
Pageable pageable = getPageable();
98100

99101
//when
100-
List<CommunityBoardView> communityBoards = communityBoardRepository.getCommunityBoards();
102+
Page<CommunityBoardView> communityBoards = communityBoardRepository.findCommunityBoards(pageable);
101103

102104
//then
103-
assertThat(communityBoards).hasSize(2);
104-
assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId());
105-
assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
106-
assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname());
107-
assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
108-
assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId());
109-
assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
110-
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
111-
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
105+
assertThat(communityBoards).isNotNull();
106+
assertThat(communityBoards.getTotalElements()).isEqualTo(28);
107+
assertThat(communityBoards.getSize()).isEqualTo(10);
108+
assertThat(communityBoards.getNumber()).isZero();
112109
}
113110

114111
@DisplayName("저장된 커뮤니티 게시글 리스트를 작성자별로 조회할 수 있다. (Repository)")
115112
@Test
116113
void getCommunityBoardsByWriterId() {
117114
//given
118-
String oAuthId = "example-oauth-id";
119-
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
120-
volunteerRepository.save(volunteer);
121-
122-
CommunityBoard communityBoard1 = CommunityBoard.builder()
123-
.title("테스트 커뮤니티 게시글 제목1")
124-
.content("테스트 커뮤니티 게시글 내용1")
125-
.imgUrl("http://community.example.com/123")
126-
.writerId(volunteer.getId())
127-
.build();
128-
129-
CommunityBoard communityBoard2 = CommunityBoard.builder()
130-
.title("테스트 커뮤니티 게시글 제목2")
131-
.content("테스트 커뮤니티 게시글 내용2")
132-
.imgUrl("http://community.example.com/12")
133-
.writerId(volunteer.getId())
134-
.build();
135-
136-
CommunityBoard communityBoard3 = CommunityBoard.builder()
137-
.title("테스트 커뮤니티 게시글 제목3")
138-
.content("테스트 커뮤니티 게시글 내용3")
139-
.imgUrl("http://community.example.com/1")
140-
.writerId(UUID.randomUUID())
141-
.build();
142-
143-
communityBoardRepository.save(communityBoard1);
144-
communityBoardRepository.save(communityBoard2);
145-
communityBoardRepository.save(communityBoard3);
115+
Pageable pageable = getPageable();
146116

147117
//when
148-
List<CommunityBoardView> communityBoards = communityBoardRepository.findByWriterId(volunteer.getId());
118+
Page<CommunityBoardView> communityBoards = communityBoardRepository.findByWriterId(writerId, pageable);
149119

150120
//then
151-
assertThat(communityBoards).hasSize(2);
152-
assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId());
153-
assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
154-
assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname());
155-
assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
156-
assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId());
157-
assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
158-
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
159-
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
121+
assertThat(communityBoards).isNotNull();
122+
assertThat(communityBoards.getTotalElements()).isEqualTo(8);
123+
assertThat(communityBoards.getSize()).isEqualTo(10);
124+
assertThat(communityBoards.getNumber()).isZero();
160125
}
161126

162127
@DisplayName("게시글 id로 게시글이 존재하는지 확인할 수 있다.")
163128
@Test
164129
void existsById() {
165-
166130
//given
167-
UUID writerId = UUID.randomUUID();
168-
169-
CommunityBoard communityBoard = CommunityBoard.builder()
170-
.title("테스트 커뮤니티 게시글 제목")
171-
.content("테스트 커뮤니티 게시글 내용")
172-
.imgUrl("http://community.example.com/123")
173-
.writerId(writerId)
174-
.build();
175-
176-
CommunityBoard savedComment = communityBoardRepository.save(communityBoard);
131+
CommunityBoard communityBoard = createCommunityBoard();
132+
communityBoardRepository.save(communityBoard);
177133

178134
//when
179-
boolean isExist = communityBoardRepository.existsById(savedComment.getId());
135+
boolean isExist = communityBoardRepository.existsById(communityBoard.getId());
180136

181137
//then
182138
assertThat(isExist).isTrue();
183139
}
140+
141+
private Pageable getPageable() {
142+
return PageRequest.of(0, 10);
143+
}
184144
}

src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
import org.junit.jupiter.api.DisplayName;
1414
import org.junit.jupiter.api.Test;
1515
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.data.domain.Page;
17+
import org.springframework.data.domain.PageRequest;
18+
import org.springframework.data.domain.Pageable;
1619
import org.springframework.transaction.annotation.Transactional;
1720

18-
import java.util.List;
1921
import java.util.Optional;
2022
import java.util.UUID;
2123

2224
import static org.assertj.core.api.Assertions.assertThat;
25+
import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard;
26+
import static com.somemore.common.fixture.CommunityCommentFixture.createCommunityComment;
2327

2428
@Transactional
2529
class CommunityCommentRepositoryTest extends IntegrationTestSupport {
@@ -40,34 +44,19 @@ void setUp() {
4044
String oAuthId = "example-oauth-id";
4145
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
4246
volunteerRepository.save(volunteer);
43-
4447
writerId = volunteer.getId();
4548

46-
CommunityBoard communityBoard = CommunityBoard.builder()
47-
.title("테스트 커뮤니티 게시글 제목")
48-
.content("테스트 커뮤니티 게시글 내용")
49-
.imgUrl("http://community.example.com/123")
50-
.writerId(writerId)
51-
.build();
52-
49+
CommunityBoard communityBoard = createCommunityBoard(writerId);
5350
communityBoardRepository.save(communityBoard);
54-
5551
boardId = communityBoard.getId();
5652

57-
CommunityComment communityComment = CommunityComment.builder()
58-
.communityBoardId(boardId)
59-
.writerId(writerId)
60-
.content("커뮤니티 댓글 테스트 내용")
61-
.parentCommentId(null)
62-
.build();
63-
53+
CommunityComment communityComment = createCommunityComment(boardId, writerId);
6454
savedComment = communityCommentRepository.save(communityComment);
6555
}
6656

6757
@DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)")
6858
@Test
69-
void createCommunityComment() {
70-
59+
void createParentCommunityComment() {
7160
//given
7261
//when
7362
//then
@@ -79,14 +68,8 @@ void createCommunityComment() {
7968
@DisplayName("댓글에 대댓글을 생성할 수 있다. (Repository)")
8069
@Test
8170
void createCommunityCommentReply() {
82-
8371
//given
84-
CommunityComment communityCommentReply = CommunityComment.builder()
85-
.communityBoardId(boardId)
86-
.writerId(writerId)
87-
.content("커뮤니티 댓글 테스트 내용")
88-
.parentCommentId(savedComment.getId())
89-
.build();
72+
CommunityComment communityCommentReply = createCommunityComment(boardId, writerId, savedComment.getId());
9073

9174
//when
9275
CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply);
@@ -100,7 +83,6 @@ void createCommunityCommentReply() {
10083
@DisplayName("댓글을 id로 조회할 수 있다. (Repository)")
10184
@Test
10285
void findCommunityCommentById() {
103-
10486
//given
10587
//when
10688
Optional<CommunityComment> comment = communityCommentRepository.findById(savedComment.getId());
@@ -115,7 +97,6 @@ void findCommunityCommentById() {
11597
@DisplayName("댓글 id로 댓글이 존재하는지 확인할 수 있다.")
11698
@Test
11799
void existsById() {
118-
119100
//given
120101
//when
121102
boolean isExist = communityCommentRepository.existsById(savedComment.getId());
@@ -124,26 +105,29 @@ void existsById() {
124105
assertThat(isExist).isTrue();
125106
}
126107

127-
@DisplayName("게시글 id로 게시글에 달린 댓글을 조회할 수 있다. (Repository)")
108+
@DisplayName("게시글 id로 게시글에 달린 댓글을 페이지로 조회할 수 있다. (Repository)")
128109
@Test
129110
void findCommunityCommentByBoardId() {
130-
131111
//given
132-
CommunityComment communityCommentReply = CommunityComment.builder()
133-
.communityBoardId(boardId)
134-
.writerId(writerId)
135-
.content("커뮤니티 댓글 테스트 내용")
136-
.parentCommentId(savedComment.getId())
137-
.build();
112+
for (int i = 1; i <= 8; i++) {
113+
String content = "제목" + i;
114+
CommunityComment communityComment = createCommunityComment(content, boardId, writerId);
115+
communityCommentRepository.save(communityComment);
116+
}
138117

139-
CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply);
118+
Pageable pageable = getPageable();
140119

141120
//when
142-
List<CommunityCommentView> comments = communityCommentRepository.findCommentsByBoardId(boardId);
121+
Page<CommunityCommentView> comments = communityCommentRepository.findCommentsByBoardId(boardId, pageable);
143122

144123
//then
145-
assertThat(comments).hasSize(2);
146-
assertThat(comments.getFirst().communityComment().getId()).isEqualTo(savedComment.getId());
147-
assertThat(comments.getLast().communityComment().getId()).isEqualTo(savedCommentReply.getId());
124+
assertThat(comments).isNotNull();
125+
assertThat(comments.getTotalElements()).isEqualTo(9);
126+
assertThat(comments.getSize()).isEqualTo(4);
127+
assertThat(comments.getNumber()).isZero();
128+
}
129+
130+
private Pageable getPageable() {
131+
return PageRequest.of(0, 4);
148132
}
149133
}

0 commit comments

Comments
 (0)