Skip to content

Commit 7404b0f

Browse files
committed
refactor: Separate comment-related test code from main class
1 parent 0f4ae6a commit 7404b0f

File tree

4 files changed

+170
-55
lines changed

4 files changed

+170
-55
lines changed

src/main/java/com/gdsc/blog/comment/repository/CommentRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88

99
public interface CommentRepository extends JpaRepository<Comment, Long> {
1010
List<Comment> findByArticle(Article article);
11+
12+
List<Comment> findByArticle_Idx(Long idx);
13+
1114
}

src/main/java/com/gdsc/blog/comment/service/CommentService.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public Comment create(Comment comment, Long id, User user) {
3636
return commentRepository.save(comment);
3737
}
3838

39-
public List<Comment> getAllCommandByArticle(Article article) {
40-
return commentRepository.findByArticle(article);
39+
public List<Comment> getAllCommandByArticle(Long articleId) {
40+
return commentRepository.findByArticle_Idx(articleId);
4141
}
4242

4343
/**
@@ -49,7 +49,8 @@ public List<Comment> getAllCommandByArticle(Article article) {
4949
public Comment getCommentById(Long idx) {
5050
Optional<Comment> comment = commentRepository.findById(idx);
5151

52-
return comment.orElseThrow(() -> new NullPointerException("Not found Comment by id"));
52+
return comment.orElseThrow();
53+
5354
}
5455

5556
/**

src/test/java/com/gdsc/blog/BlogApplicationTests.java

-52
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,4 @@ class BlogApplicationTests {
2626
void contextLoads() {
2727
assertTrue(true);
2828
}
29-
30-
@Autowired
31-
private UserController userController;
32-
33-
34-
@Autowired
35-
private ArticleRepository articleRepository;
36-
37-
@Autowired
38-
private ArticleService articleService;
39-
40-
@Autowired
41-
private CommentRepository commentRepository;
42-
@Autowired
43-
private CommentService commentService;
44-
45-
@Test
46-
void testComment() {
47-
//create new post
48-
Article post1 = new Article();
49-
//set title and content
50-
post1.setTitle("Post1 title");
51-
post1.setContent("Post1 content");
52-
post1.setCreateDate(LocalDateTime.now());
53-
//save post
54-
this.articleRepository.save(post1);
55-
56-
//create new comment
57-
for (int i = 0; i < 3; i++) {
58-
this.commentService.create(post1, "post1 comment" + String.valueOf(i + 1));
59-
}
60-
61-
//get comment by article
62-
Long articleIdx = post1.getIdx();
63-
List<Comment> comments = this.commentRepository.findByArticleIdx(articleIdx);
64-
assertEquals(3, comments.size()); //check comment count
65-
66-
//get comment content
67-
Comment c = this.commentService.getComment(Long.valueOf(3)); //get comment by id
68-
String content = c.getContent(); //get comment content
69-
assertEquals("post1 comment3", content); //check comment content
70-
71-
//update comment
72-
c.setContent("post1 comment3 updated");
73-
c.setModifyData(LocalDateTime.now());
74-
this.commentService.update(c);
75-
assertEquals("post1 comment3 updated", c.getContent()); //check updated content
76-
77-
//delete comment
78-
this.commentService.delete(c);
79-
assertEquals(2, this.commentRepository.count()); //check comment count
80-
}
8129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.gdsc.blog.comment.service;
2+
3+
import com.gdsc.blog.article.dto.ArticleCreateDto;
4+
import com.gdsc.blog.article.dto.ArticleDto;
5+
import com.gdsc.blog.article.service.ArticleService;
6+
import com.gdsc.blog.comment.dto.CommentUpdateDto;
7+
import com.gdsc.blog.comment.entity.Comment;
8+
import com.gdsc.blog.user.entity.User;
9+
import com.gdsc.blog.user.entity.UserRole;
10+
import com.gdsc.blog.user.service.UserService;
11+
import jakarta.transaction.Transactional;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.junit.jupiter.api.BeforeAll;
14+
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.TestInstance;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.context.annotation.Profile;
20+
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.NoSuchElementException;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
28+
@Slf4j
29+
@SpringBootTest
30+
@Profile("test")
31+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
32+
class CommentServiceTest {
33+
34+
@Autowired
35+
private UserService userService;
36+
37+
@Autowired
38+
private ArticleService articleService;
39+
40+
@Autowired
41+
private CommentService commentService;
42+
43+
User user;
44+
User admin;
45+
ArticleDto articleDto;
46+
47+
@BeforeAll
48+
void setUp() {
49+
// 회원 가입
50+
user = userService.signup(User.builder()
51+
.username("user1")
52+
53+
.password("password")
54+
.build());
55+
admin = userService.signup(User.builder()
56+
.username("admin1")
57+
58+
.password("password")
59+
.roles(Collections.singletonList(UserRole.ROLE_ADMIN))
60+
.build());
61+
userService.becomeAdmin(admin.getUsername());
62+
63+
// 게시글 생성
64+
articleDto = articleService.createArticle(ArticleCreateDto.builder()
65+
.title("title")
66+
.content("content")
67+
.build(), user.getUsername());
68+
69+
}
70+
71+
@Test
72+
@Transactional
73+
@DisplayName("댓글 생성")
74+
void create() {
75+
Comment comment = Comment.builder()
76+
.content("content")
77+
.build();
78+
Comment saved = commentService.create(comment, articleDto.getIdx(), user);
79+
assertEquals(comment.getContent(), saved.getContent());
80+
}
81+
82+
@Test
83+
@Transactional
84+
@DisplayName("게시글 별 댓글 조회")
85+
void getAllCommandByArticle() {
86+
// given
87+
Comment comment1 = Comment.builder()
88+
.content("content1")
89+
.build();
90+
Comment comment2 = Comment.builder()
91+
.content("content2")
92+
.build();
93+
Comment comment3 = Comment.builder()
94+
.content("content3")
95+
.build();
96+
commentService.create(comment1, articleDto.getIdx(), user);
97+
commentService.create(comment2, articleDto.getIdx(), user);
98+
commentService.create(comment3, articleDto.getIdx(), user);
99+
100+
// when
101+
List<Comment> comments = commentService.getAllCommandByArticle(articleDto.getIdx());
102+
103+
// then
104+
assertEquals(3, comments.size());
105+
}
106+
107+
@Test
108+
@Transactional
109+
@DisplayName("특정 댓글 조회")
110+
void getCommentById() {
111+
// given
112+
Comment comment = Comment.builder()
113+
.content("content")
114+
.build();
115+
Comment saved = commentService.create(comment, articleDto.getIdx(), user);
116+
117+
// when
118+
Comment found = commentService.getCommentById(saved.getIdx());
119+
120+
// then
121+
assertEquals(saved.getIdx(), found.getIdx());
122+
assertEquals(saved.getContent(), found.getContent());
123+
}
124+
125+
@Test
126+
@Transactional
127+
@DisplayName("댓글 수정")
128+
void updateComment() {
129+
// given
130+
Comment comment = Comment.builder()
131+
.content("content")
132+
.build();
133+
Comment saved = commentService.create(comment, articleDto.getIdx(), user);
134+
135+
CommentUpdateDto commentUpdateDto = CommentUpdateDto.builder()
136+
.content("updated")
137+
.build();
138+
139+
// when
140+
Comment updated = commentService.updateComment(saved.getIdx(), commentUpdateDto);
141+
142+
// then
143+
assertEquals(saved.getIdx(), updated.getIdx());
144+
assertEquals("updated", updated.getContent());
145+
}
146+
147+
@Test
148+
@DisplayName("댓글 삭제")
149+
@Transactional
150+
void delete() {
151+
// given
152+
Comment comment = Comment.builder()
153+
.content("content")
154+
.build();
155+
Comment saved = commentService.create(comment, articleDto.getIdx(), user);
156+
157+
// when
158+
commentService.delete(saved);
159+
160+
// then
161+
assertThrows(NoSuchElementException.class, () -> commentService.getCommentById(saved.getIdx()));
162+
}
163+
}

0 commit comments

Comments
 (0)