-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
전반적인 테스트코드 개선
- Loading branch information
Showing
8 changed files
with
150 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
import com.example.inflearn.infra.repository.comment.CommentRepository; | ||
import com.example.inflearn.infra.repository.member.MemberRepository; | ||
import com.example.inflearn.infra.repository.post.PostRepository; | ||
import com.navercorp.fixturemonkey.FixtureMonkey; | ||
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
|
@@ -30,33 +32,29 @@ | |
@ExtendWith(MockitoExtension.class) | ||
class CommentServiceTest { | ||
|
||
private final Member member = createMember(); | ||
private final Post post = createPost(); | ||
private final Comment comment = Comment.createComment(member, post, "댓글내용1"); | ||
|
||
@InjectMocks | ||
private CommentService sut; | ||
|
||
@Mock | ||
private MemberRepository memberRepository; | ||
|
||
@Mock | ||
private PostRepository postRepository; | ||
|
||
@Mock | ||
private CommentRepository commentRepository; | ||
|
||
@Test | ||
void 게시글_댓글_작성_성공() { | ||
// given | ||
long memberId = 1L; | ||
long postId = 1L; | ||
given(memberRepository.findById(memberId)).willReturn(Optional.ofNullable(member)); | ||
given(postRepository.findById(postId)).willReturn(Optional.ofNullable(post)); | ||
private final FixtureMonkey fixtureMonkey = FixtureMonkey.builder() | ||
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE) | ||
.build(); | ||
|
||
// when | ||
sut.saveComment(memberId, postId, comment.getContents()); | ||
private final Member member = fixtureMonkey.giveMeBuilder(Member.class) | ||
.setNotNull("id").sample(); | ||
|
||
// then | ||
then(commentRepository).should().save(any(Comment.class)); | ||
} | ||
private final Post post = fixtureMonkey.giveMeBuilder(Post.class) | ||
.setNotNull("id").sample(); | ||
|
||
private final Comment comment = Comment.createComment(member, post, "댓글내용1"); | ||
|
||
@Test | ||
void 게시글_댓글_작성_실패_존재하지_않는_게시글() { | ||
|
@@ -96,7 +94,6 @@ class CommentServiceTest { | |
long commentId = 1L; | ||
Comment parentComment = Comment.createComment(member, post, "댓글내용1"); | ||
Comment reply = Comment.createComment(member, parentComment.getPost(), "답글내용1"); | ||
|
||
given(memberRepository.findById(memberId)).willReturn(Optional.ofNullable(member)); | ||
given(commentRepository.findById(commentId)).willReturn(Optional.of(parentComment)); | ||
|
||
|
@@ -147,18 +144,4 @@ class CommentServiceTest { | |
assertThrows(CannotCreateReplyException.class, | ||
() -> sut.saveReply(memberId, commentId, comment.getContents())); | ||
} | ||
|
||
private Member createMember() { | ||
return Member.builder() | ||
.email("[email protected]") | ||
.password("12345678") | ||
.build(); | ||
} | ||
|
||
private Post createPost() { | ||
return Post.builder() | ||
.title("글제목1") | ||
.contents("글본문1") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import com.example.inflearn.infra.repository.like.LikeRepository; | ||
import com.example.inflearn.infra.repository.member.MemberRepository; | ||
import com.example.inflearn.infra.repository.post.PostRepository; | ||
import com.navercorp.fixturemonkey.FixtureMonkey; | ||
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
|
@@ -28,9 +30,6 @@ | |
@ExtendWith(MockitoExtension.class) | ||
class LikeServiceTest { | ||
|
||
private final Member member = getMember(); | ||
private final Post post = getEntity(); | ||
|
||
@InjectMocks | ||
private LikeService sut; | ||
|
||
|
@@ -41,6 +40,15 @@ class LikeServiceTest { | |
@Mock | ||
private LikeRepository likeRepository; | ||
|
||
private final FixtureMonkey fixtureMonkey = FixtureMonkey.builder() | ||
.objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE) | ||
.build(); | ||
|
||
private final Member member = fixtureMonkey.giveMeBuilder(Member.class) | ||
.setNotNull("id").sample(); | ||
private final Post post = fixtureMonkey.giveMeBuilder(Post.class) | ||
.setNotNull("id").sample(); | ||
|
||
@Test | ||
void 게시글_좋아요_성공() { | ||
// given | ||
|
@@ -61,23 +69,17 @@ class LikeServiceTest { | |
given(memberRepository.findById(member.getId())).willReturn(Optional.of(member)); | ||
given(postRepository.findById(post.getId())).willReturn(Optional.empty()); | ||
|
||
// when | ||
// when & then | ||
assertThrows(DoesNotExistPostException.class, () -> sut.saveLike(member.getId(), post.getId())); | ||
|
||
// then | ||
then(likeRepository).shouldHaveNoMoreInteractions(); | ||
} | ||
|
||
@Test | ||
void 존재하지_않는_유저는_좋아요_누를_수_없다() { | ||
// given | ||
given(memberRepository.findById(member.getId())).willReturn(Optional.empty()); | ||
|
||
// when | ||
// when & then | ||
assertThrows(DoesNotExistMemberException.class, () -> sut.saveLike(member.getId(), post.getId())); | ||
|
||
// then | ||
then(likeRepository).shouldHaveNoMoreInteractions(); | ||
} | ||
|
||
@Test | ||
|
@@ -87,11 +89,8 @@ class LikeServiceTest { | |
given(postRepository.findById(post.getId())).willReturn(Optional.of(post)); | ||
given(likeRepository.findByMemberAndPost(member, post)).willReturn(Like.create(member, post)); | ||
|
||
// when | ||
// when & then | ||
assertThrows(AlreadyLikeException.class, () -> sut.saveLike(member.getId(), post.getId())); | ||
|
||
// then | ||
then(likeRepository).shouldHaveNoMoreInteractions(); | ||
} | ||
|
||
@Test | ||
|
@@ -116,26 +115,7 @@ class LikeServiceTest { | |
given(postRepository.findById(post.getId())).willReturn(Optional.of(post)); | ||
given(likeRepository.findByMemberAndPost(member, post)).willReturn(null); | ||
|
||
// when | ||
// when & then | ||
assertThrows(DoesNotLikeException.class, () -> sut.unLike(member.getId(), post.getId())); | ||
|
||
// then | ||
then(likeRepository).shouldHaveNoMoreInteractions(); | ||
} | ||
|
||
private Member getMember() { | ||
return Member.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password("12345678") | ||
.build(); | ||
} | ||
|
||
private Post getEntity() { | ||
return Post.builder() | ||
.id(1L) | ||
.title("제목1234") | ||
.contents("본문1234") | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.