-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ssafy-19-final-pjt/test/comment-testcode
Test : comment testcode 추가
- Loading branch information
Showing
8 changed files
with
508 additions
and
24 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/ssafy/home/domain/comment/dto/request/CommentRequestUpdateDto.java
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package com.ssafy.home.domain.comment.dto.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentRequestUpdateDto { | ||
private String content; | ||
|
||
@Builder | ||
private CommentRequestUpdateDto(String content) { | ||
this.content = content; | ||
} | ||
} |
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
111 changes: 111 additions & 0 deletions
111
backend/src/test/java/com/ssafy/home/domain/comment/service/CommentReadServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.ssafy.home.domain.comment.service; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
import com.ssafy.home.config.TestConfig; | ||
import com.ssafy.home.domain.board.entity.Board; | ||
import com.ssafy.home.domain.board.repository.BoardRepository; | ||
import com.ssafy.home.domain.comment.entity.Comment; | ||
import com.ssafy.home.domain.comment.repository.CommentRepository; | ||
import com.ssafy.home.domain.member.repository.MemberRepository; | ||
import com.ssafy.home.entity.member.Member; | ||
import com.ssafy.home.global.error.ErrorCode; | ||
import com.ssafy.home.global.error.exception.BadRequestException; | ||
import groovy.util.logging.Slf4j; | ||
import java.util.List; | ||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Transactional | ||
class CommentReadServiceTest extends TestConfig { | ||
private final CommentReadService commentReadService; | ||
private final MemberRepository memberRepository; | ||
private final BoardRepository boardRepository; | ||
private final CommentRepository commentRepository; | ||
|
||
private Member member; | ||
private Board board; | ||
private Comment comment; | ||
|
||
@Autowired | ||
public CommentReadServiceTest(CommentReadService commentReadService, MemberRepository memberRepository, | ||
BoardRepository boardRepository, CommentRepository commentRepository) { | ||
this.commentReadService = commentReadService; | ||
this.memberRepository = memberRepository; | ||
this.boardRepository = boardRepository; | ||
this.commentRepository = commentRepository; | ||
} | ||
|
||
@BeforeEach | ||
void before() { | ||
member = memberRepository.findById(11L).orElseThrow(IllegalStateException::new); | ||
board = boardRepository.save(Board.builder() | ||
.content("test") | ||
.member(member) | ||
.title("testTitled") | ||
.build()); | ||
comment = commentRepository.save(Comment.builder() | ||
.content("test") | ||
.member(member) | ||
.board(board) | ||
.build()); | ||
commentRepository.save(Comment.builder() | ||
.content("test2") | ||
.member(member) | ||
.board(board) | ||
.build()); | ||
} | ||
|
||
@Nested | ||
@DisplayName("댓글 읽기 기능 테스트") | ||
class CommentRead { | ||
@Test | ||
void 성공_게시판_ID를_입력_시_게시판에_작성된_댓글을_가져온다() { | ||
//given | ||
//when | ||
List<Comment> result = commentReadService.getCommentFromBoardId(board.getId()); | ||
|
||
//then | ||
SoftAssertions.assertSoftly(softAssertions -> { | ||
softAssertions.assertThat(result).isNotNull() | ||
.hasSize(2) | ||
.allMatch(comment -> comment.getMember().getId().equals(member.getId())) | ||
.allMatch(comment -> comment.getContent().startsWith("test")); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
void 성공_댓글_ID를_입력_시_작성된_댓글을_가져온다() { | ||
//given | ||
//when | ||
Comment result = commentReadService.getComment(comment.getId()); | ||
|
||
//then | ||
SoftAssertions.assertSoftly(softAssertions -> { | ||
softAssertions.assertThat(result).isNotNull(); | ||
softAssertions.assertThat(result.getBoard().getId()).isEqualTo(board.getId()); | ||
softAssertions.assertThat(result.getMember().getId()).isEqualTo(member.getId()); | ||
softAssertions.assertThat(result.getContent()).isEqualTo(comment.getContent()); | ||
}); | ||
} | ||
|
||
@Test | ||
void 실패_댓글_ID가_존재하지_않는_경우_예외가_발생한다() { | ||
//given | ||
Long noneExistCommentID = 0L; | ||
|
||
//when then | ||
assertThatThrownBy(() -> commentReadService.getComment(noneExistCommentID)) | ||
.isInstanceOf(BadRequestException.class) | ||
.extracting("errorCode") | ||
.isEqualTo(ErrorCode.COMMENT_NOT_FOUND); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
backend/src/test/java/com/ssafy/home/domain/comment/service/CommentResponseMapperTest.java
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.ssafy.home.domain.comment.service; | ||
|
||
import com.ssafy.home.config.TestConfig; | ||
import com.ssafy.home.domain.board.entity.Board; | ||
import com.ssafy.home.domain.comment.dto.response.CommentResponseDto; | ||
import com.ssafy.home.domain.comment.entity.Comment; | ||
import com.ssafy.home.entity.member.Member; | ||
import groovy.util.logging.Slf4j; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Transactional | ||
@ExtendWith(MockitoExtension.class) | ||
class CommentResponseMapperTest extends TestConfig { | ||
private static final Logger log = LoggerFactory.getLogger(CommentResponseMapperTest.class); | ||
@InjectMocks | ||
private CommentResponseMapper commentResponseMapper; | ||
|
||
@Mock | ||
private Member member; | ||
|
||
@Mock | ||
private Board board; | ||
|
||
private List<Comment> comments; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
comments = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
comments.add(Comment.builder().content("Content " + i).member(member).board(board).build()); | ||
} | ||
} | ||
|
||
@Test | ||
void 성공_댓글_리스트를_받을_시_응답_형식에_맞게_매핑된다() { | ||
// given | ||
// when | ||
List<CommentResponseDto> result = commentResponseMapper.toListCommentResponse(comments); | ||
|
||
// then | ||
SoftAssertions.assertSoftly(softAssertions -> { | ||
softAssertions.assertThat(result).isNotNull() | ||
.hasSize(5) | ||
.allMatch(dto -> dto.getContent().startsWith("Content")); | ||
}); | ||
} | ||
} |
Oops, something went wrong.