Skip to content

Commit

Permalink
KAN-111 Feat: Fit Test with Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawardshin committed Sep 1, 2024
1 parent f048fa2 commit ac86c00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.HttpStatus;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.server.ResponseStatusException;

import capstone.relation.meeting.domain.MeetRoom;
import capstone.relation.meeting.dto.request.CreateRoomDto;
import capstone.relation.meeting.dto.response.JoinResponseDto;
import capstone.relation.meeting.dto.response.MeetingRoomListDto;
import capstone.relation.meeting.exception.MeetingException;
import capstone.relation.meeting.repository.MeetRoomRepository;
import capstone.relation.meeting.repository.RedisRepository;
import capstone.relation.security.WithMockCustomUser;
Expand Down Expand Up @@ -82,12 +81,7 @@ public void createAndJoinRoomWithInvalidRoomName() {

// when & then
assertThatThrownBy(() -> meetRoomService.createAndJoinRoom(1L, createRoomDto))
.isInstanceOf(ResponseStatusException.class)
.satisfies(exception -> {
ResponseStatusException ex = (ResponseStatusException)exception;
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(ex.getReason()).isEqualTo("회의실 이름을 입력해주세요.");
});
.isInstanceOf(MeetingException.class);
}

@DisplayName("이미 회의실에 참여한 사용자가 다시 참여하려고 할 때 예외가 발생한다.")
Expand All @@ -103,8 +97,7 @@ public void createAndJoinRoomWithAlreadyJoinedUser() {

// when & then
assertThatThrownBy(() -> meetRoomService.createAndJoinRoom(1L, createRoomDto))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("User is already in the room: 1");
.isInstanceOf(MeetingException.class);
}

@DisplayName("워크스페이스에 참여한 모든 유저에게 회의실 목록을 전송할 수 있다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import capstone.relation.api.auth.exception.AuthException;
import capstone.relation.user.domain.User;
import capstone.relation.user.exception.UserException;
import capstone.relation.user.repository.UserRepository;
import capstone.relation.websocket.chat.domain.Chat;
import capstone.relation.websocket.chat.dto.response.HistoryResponseDto;
import capstone.relation.websocket.chat.dto.response.MessageDto;
import capstone.relation.websocket.chat.repository.ChatRepository;
import capstone.relation.workspace.WorkSpace;
import capstone.relation.workspace.exception.WorkSpaceException;

@ExtendWith(MockitoExtension.class)
@DisplayName("ChatService 단위 테스트")
Expand Down Expand Up @@ -81,28 +82,28 @@ void whenUserIdIsValidAndWorkSpaceExists_thenReturnHistory() {
}

@Test
@DisplayName("사용자 ID가 유효하지 않으면 AuthException을 던진다.")
@DisplayName("사용자 ID가 유효하지 않으면 User Exception 을 던진다.")
void whenUserIdIsInvalid_thenThrowAuthException() {
// given
Long userId = 1L;
when(userRepository.findById(userId)).thenReturn(Optional.empty());

// when & then
assertThrows(AuthException.class, () -> chatService.getHistory(null, userId));
assertThrows(UserException.class, () -> chatService.getHistory(null, userId));

verify(userRepository, times(1)).findById(userId);
}

@Test
@DisplayName("작업 공간이 없으면 AuthException을 던진다.")
@DisplayName("작업 공간이 없으면 WorkSpace Exception 을 던진다.")
void whenUserHasNoWorkSpace_thenThrowAuthException() {
// given
Long userId = 1L;
validUser.setWorkSpace(null);
mockUserRepository(userId);

// when & then
assertThrows(AuthException.class, () -> chatService.getHistory(null, userId));
assertThrows(WorkSpaceException.class, () -> chatService.getHistory(null, userId));

verify(userRepository, times(1)).findById(userId);
}
Expand Down

0 comments on commit ac86c00

Please sign in to comment.