Skip to content

Commit

Permalink
Merge pull request #31 from youngreal/refactor/#30
Browse files Browse the repository at this point in the history
전반적인 테스트코드 개선
  • Loading branch information
youngreal authored Aug 28, 2024
2 parents b7c3164 + 46f3bb6 commit 9a36b01
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 446 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ dependencies {
// shedLock
implementation 'net.javacrumbs.shedlock:shedlock-spring:5.11.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:5.11.0'

// fixture monkey
testRuntimeOnly("org.junit.platform:junit-platform-launcher:{version}")
testImplementation "com.navercorp.fixturemonkey:fixture-monkey-starter:1.0.23"

// auto Params
testImplementation 'io.github.autoparams:autoparams:8.3.0'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -61,6 +69,8 @@ dependencies {
testImplementation "org.testcontainers:testcontainers:1.19.1"
testImplementation "org.testcontainers:junit-jupiter:1.19.1"
testImplementation "org.testcontainers:mysql:1.19.1"


}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public class PopularPost {
private long postId;

private long likeCount;

private PopularPost(long postId, long likeCount) {
this.postId = postId;
this.likeCount = likeCount;
}

public static PopularPost of(long postId) {
return new PopularPost(postId, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 게시글_댓글_작성_실패_존재하지_않는_게시글() {
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,9 +30,6 @@
@ExtendWith(MockitoExtension.class)
class LikeServiceTest {

private final Member member = getMember();
private final Post post = getEntity();

@InjectMocks
private LikeService sut;

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
Loading

0 comments on commit 9a36b01

Please sign in to comment.