Skip to content

Commit

Permalink
[test] : 채팅 요청 생성 실패 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyun2371 committed Jan 4, 2025
1 parent 29b7ae8 commit 185570d
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.data.domain.SliceImpl;
import org.springframework.test.util.ReflectionTestUtils;

import com.dnd.gongmuin.answer.repository.AnswerRepository;
import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
Expand All @@ -27,6 +28,7 @@
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.RejectChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.RejectedChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode;
import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository;
import com.dnd.gongmuin.chatroom.domain.ChatRoom;
import com.dnd.gongmuin.chatroom.repository.ChatMessageRepository;
Expand Down Expand Up @@ -65,6 +67,9 @@ class ChatInquiryServiceTest {
@Mock
private QuestionPostRepository questionPostRepository;

@Mock
private AnswerRepository answerRepository;

@Mock
private ApplicationEventPublisher eventPublisher;

Expand Down Expand Up @@ -98,6 +103,8 @@ void createInquiry() {
.willReturn(Optional.of(questionPost));
given(memberRepository.findById(answerer.getId()))
.willReturn(Optional.of(answerer));
given(answerRepository.existsByQuestionPostIdAndMember(questionPost.getId(), answerer))
.willReturn(true);
given(chatInquiryRepository.save(any(ChatInquiry.class))).willReturn(chatInquiry);

CreateChatInquiryResponse response = chatInquiryService.createChatInquiry(request, inquirer);
Expand Down Expand Up @@ -129,13 +136,42 @@ void createInquiry_fails() {
.willReturn(Optional.of(questionPost));
given(memberRepository.findById(answerer.getId()))
.willReturn(Optional.of(answerer));
given(answerRepository.existsByQuestionPostIdAndMember(questionPost.getId(), answerer))
.willReturn(true);

//when & then
assertThatThrownBy(() -> chatInquiryService.createChatInquiry(request, inquirer))
.isInstanceOf(ValidationException.class)
.hasMessageContaining(MemberErrorCode.NOT_ENOUGH_CREDIT.getMessage());
}

@DisplayName("[질문 게시글에 답변을 하지 않은 회원에게 채팅 신청할 수 없다.]")
@Test
void createChatInquiry_fails2() {
//given
Member inquirer = MemberFixture.member(1L);
Member answerer = MemberFixture.member(2L);
ReflectionTestUtils.setField(inquirer, "credit", CHAT_REWARD);
QuestionPost questionPost = QuestionPostFixture.questionPost(inquirer);
CreateChatInquiryRequest request = new CreateChatInquiryRequest(
questionPost.getId(),
answerer.getId(),
INQUIRY_MESSAGE
);

given(questionPostRepository.findById(questionPost.getId()))
.willReturn(Optional.of(questionPost));
given(memberRepository.findById(answerer.getId()))
.willReturn(Optional.of(answerer));
given(answerRepository.existsByQuestionPostIdAndMember(questionPost.getId(), answerer))
.willReturn(false);

//when & then
assertThatThrownBy(() -> chatInquiryService.createChatInquiry(request, inquirer))
.isInstanceOf(ValidationException.class)
.hasMessageContaining(ChatInquiryErrorCode.NOT_EXISTS_ANSWERER.getMessage());
}

@DisplayName("[채팅 요청 아이디로 채팅 요청 상세를 조회할 수 있다.]")
@Test
void getChatInquiryById() {
Expand Down

0 comments on commit 185570d

Please sign in to comment.