Skip to content

Commit

Permalink
Test : PR Review 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
GBGreenBravo committed Jan 2, 2024
2 parents 27ff110 + 00920fb commit aa9dff7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public enum CommentErrorCode implements ErrorCode {
NOT_FOUND(HttpStatus.NOT_FOUND.value(), "댓글이 존재하지 않습니다."),
FORBIDDEN(HttpStatus.FORBIDDEN.value(), "댓글 수정/삭제 권한이 없습니다."),
BAD_REQUEST(HttpStatus.BAD_REQUEST.value(), "잘못된 댓글 요청입니다.");
BAD_REQUEST(HttpStatus.BAD_REQUEST.value(), "잘못된 댓글 생성 요청입니다.");

private final int status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import org.fastcampus.orurydomain.comment.dto.CommentLikeDto;
import org.fastcampus.orurydomain.post.db.model.Post;
import org.fastcampus.orurydomain.user.db.model.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;

Expand All @@ -31,13 +30,18 @@
@DisplayName("CommentLikeServiceTest")
@ActiveProfiles("test")
class CommentLikeServiceTest {
@Mock

private CommentLikeRepository commentLikeRepository;
@Mock
private CommentRepository commentRepository;
@InjectMocks
private CommentLikeService commentLikeService;

@BeforeEach
void setUp() {
commentLikeRepository = mock(CommentLikeRepository.class);
commentRepository = mock(CommentRepository.class);
commentLikeService = new CommentLikeService(commentLikeRepository, commentRepository);
}

@Test
@DisplayName("댓글에 좋아요를 누르면, DB에 데이터가 생성되고 댓글에 좋아요 개수를 증가시킨다.")
void when_LikeComment_Then_CreateCommentLikeAndIncreaseCommentLikeCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import org.fastcampus.orurydomain.post.dto.PostDto;
import org.fastcampus.orurydomain.user.db.model.User;
import org.fastcampus.orurydomain.user.dto.UserDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -30,23 +29,28 @@
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("CommentServiceTest")
@ActiveProfiles("test")
class CommentServiceTest {

@Mock
private CommentRepository commentRepository;
@Mock
private CommentLikeRepository commentLikeRepository;
@Mock
private PostRepository postRepository;
@InjectMocks
private CommentService commentService;

@BeforeEach
void setUp() {
commentRepository = mock(CommentRepository.class);
commentLikeRepository = mock(CommentLikeRepository.class);
postRepository = mock(PostRepository.class);
commentService = new CommentService(commentRepository, commentLikeRepository, postRepository);
}

@Test
@DisplayName("댓글이 생성되고 게시글의 댓글수가 증가되어야 한다")
void should_CreateCommentAndIncreaseCommentCountOfPost() {
Expand Down Expand Up @@ -229,7 +233,7 @@ void should_ValidateCommentOfCommentLike() {
}

@Test
@DisplayName("검증할 댓글좋아요의 댓글이 존재하지 않하면, NOT_FOUND 예외를 발생시킨다.")
@DisplayName("검증할 댓글좋아요의 댓글이 존재하지 않으면, NOT_FOUND 예외를 발생시킨다.")
void when_CommentOfCommentLikeDoesNoTExists_Then_NotFoundException() {
// given
CommentLike commentLike = createCommentLike(createCommentLikePK());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import org.fastcampus.orurydomain.post.db.repository.PostRepository;
import org.fastcampus.orurydomain.post.dto.PostLikeDto;
import org.fastcampus.orurydomain.user.dto.UserDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;

Expand All @@ -26,84 +25,89 @@
@DisplayName("게시글 좋아요 관련 테스트")
@ActiveProfiles("test")
class PostLikeServiceTest {
@Mock
private PostRepository postRepository;
@Mock
private PostLikeRepository postLikeRepository;
@InjectMocks
private PostRepository postRepository;
private PostLikeService postLikeService;

@DisplayName("유저가 게시글에 좋아요를 누르면 게시물에 좋아요 개수가 증가하고 좋아요 테이블에 데이터가 생성")
@BeforeEach
void setUp() {
postLikeRepository = mock(PostLikeRepository.class);
postRepository = mock(PostRepository.class);
postLikeService = new PostLikeService(postLikeRepository, postRepository);
}

@Test
@DisplayName("유저가 게시글에 좋아요를 누르면 게시물에 좋아요 개수가 증가하고 좋아요 테이블에 데이터가 생성")
void when_UserPostLike_Then_CreatePostLikeAndIncreasePostLikeCount() {
//given
// given
PostLikePK postLikePK = createPostLikePK();
PostLikeDto postLike = createPostLike(postLikePK);
Post post = createPost();

//when
when(postRepository.findById(postLikePK.getPostId())).thenReturn(Optional.of(post));

// when
postLikeService.createPostLike(postLike);

//then
// then
verify(postLikeRepository, times(1)).save(postLike.toEntity());
verify(postRepository, times(1)).increaseLikeCount(postLikePK.getPostId());
}

@DisplayName("좋아요시 게시글이 존재하지 않으면 NOT_FOUND 예외 발생")
@Test
@DisplayName("좋아요시 게시글이 존재하지 않으면 NOT_FOUND 예외 발생")
void verify_UserPostLikeIncreaseNotExistPost_Then_ExceptionNotFound() {
//given
// given
PostLikePK postLikePK = createPostLikePK();
PostLikeDto postLike = createPostLike(postLikePK);

//then
// when & then
assertThrows(BusinessException.class, () -> postLikeService.createPostLike(postLike));
}

@DisplayName("좋아요 취소시 게시글이 존재하지 않으면 NOT_FOUND 예외 발생")
@Test
@DisplayName("좋아요 취소시 게시글이 존재하지 않으면 NOT_FOUND 예외 발생")
void verify_UserPostLikeDecreaseNotExistPost_Then_ExceptionNotFound() {
//given
// given
PostLikePK postLikePK = createPostLikePK();
PostLikeDto postLike = createPostLike(postLikePK);

//then
// when & then
assertThrows(BusinessException.class, () -> postLikeService.deletePostLike(postLike));
}

@DisplayName("유저가 게시글에 좋아요를 누르면 게시물에 좋아요 개수가 감소하고 좋아요 테이블에 데이터가 삭제")
@Test
@DisplayName("유저가 게시글에 좋아요를 누르면 게시물에 좋아요 개수가 감소하고 좋아요 테이블에 데이터가 삭제")
void when_UserPostLike_Then_DeletePostLikeAndDecreasePostLikeCount() {
//given
// given
PostLikePK postLikePK = createPostLikePK();
PostLikeDto postLike = createPostLike(postLikePK);
Post post = createPost();

//when
when(postRepository.findById(anyLong())).thenReturn(Optional.of(post));
when(postLikeRepository.existsByPostLikePK(any(PostLikePK.class))).thenReturn(true);

// when
postLikeService.deletePostLike(postLike);

//then
// then
verify(postLikeRepository, times(1)).delete(postLike.toEntity());
verify(postRepository, times(1)).decreaseLikeCount(postLikePK.getPostId());
}

@DisplayName("유저가 해당 게시물에 좋아요를 눌렀는지 확인 -> 좋아요")
@Test
@DisplayName("유저가 해당 게시물에 좋아요를 눌렀는지 확인 -> 좋아요")
void verify_UserIsLikedPost() {
//given
// given
Long userId = 1L;
Long postId = 1L;

//when
when(postLikeRepository.existsPostLikeByPostLikePK_UserIdAndPostLikePK_PostId(userId, postId)).thenReturn(true);

// when
postLikeService.isLiked(userId, postId);

//then
// then
verify(postLikeRepository, times(1)).existsPostLikeByPostLikePK_UserIdAndPostLikePK_PostId(userId, postId);
}

Expand Down

0 comments on commit aa9dff7

Please sign in to comment.