-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Test] 투표 마감, 상세 조회 단위 테스트 추가 #187
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,103 @@ | ||
package com.kakao.golajuma.vote.domain.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.kakao.golajuma.vote.domain.exception.CloseException; | ||
import com.kakao.golajuma.vote.domain.exception.NullException; | ||
import com.kakao.golajuma.vote.infra.entity.VoteEntity; | ||
import com.kakao.golajuma.vote.infra.repository.VoteRepository; | ||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Optional; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class CloseVoteServiceTest { | ||
@InjectMocks private CloseVoteService closeVoteService; | ||
@Mock VoteRepository voteRepository; | ||
|
||
@DisplayName("존재 하지 않는 투표에 대한 마감 실패") | ||
@DisplayName("투표 마감 정상 응답") | ||
@Test | ||
public void closeVoteTest() { | ||
// given | ||
Long userId = 1L; | ||
Long voteId = 1L; | ||
Long writerId = 1L; | ||
Long requestUserId = 1L; | ||
LocalDateTime date = LocalDateTime.now().plusMinutes(240); | ||
VoteEntity vote = VoteEntity.builder().id(voteId).userId(writerId).voteEndDate(date).build(); | ||
|
||
// when | ||
when(voteRepository.findById((Long) any())).thenReturn(Optional.of(vote)); | ||
|
||
closeVoteService.closeVote(voteId, requestUserId); | ||
// 분 단위까지만 비교 | ||
LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dTo 바꾸시는거 어떤가요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 무슨 말씀이시죠?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이름이 dTo잖아요 저거 |
||
LocalDateTime closeDate = vote.getVoteEndDate().truncatedTo(ChronoUnit.MINUTES); | ||
|
||
// then | ||
// 현재 시각과 종료된 시각이 일치하는지 검사 | ||
assertEquals(now, closeDate); | ||
} | ||
|
||
@DisplayName("존재 하지 않는 투표에 대한 마감 실패") | ||
@Test | ||
public void closeVoteTest_notFoundVote() { | ||
// given | ||
Long voteId = -1L; | ||
Long requestUserId = 1L; | ||
|
||
// when & then | ||
assertThrows(NullPointerException.class, () -> closeVoteService.closeVote(voteId, userId)); | ||
assertThrows(NullException.class, () -> closeVoteService.closeVote(voteId, requestUserId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 repository를 when then return한 부분이 안보이는거같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 나도 이렇게 했네? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??/ㅋㅋㅋㅋㅋㅋㅋ |
||
} | ||
|
||
@DisplayName("작성자가 아닌 경우 투표에 대한 마감 실패") | ||
@Test | ||
public void closeVoteTest_notWriter() { | ||
// given | ||
Long voteId = 1L; | ||
Long writerId = 1L; | ||
Long requestUserId = 2L; | ||
VoteEntity vote = | ||
VoteEntity.builder() | ||
.id(voteId) | ||
.userId(writerId) | ||
.voteEndDate(LocalDateTime.now().plusMinutes(240)) | ||
.build(); | ||
|
||
// when | ||
when(voteRepository.findById((Long) any())).thenReturn(Optional.of(vote)); | ||
|
||
// then | ||
assertThrows(CloseException.class, () -> closeVoteService.closeVote(voteId, requestUserId)); | ||
} | ||
|
||
@DisplayName("이미 완료된 투표인 경우 마감 실패") | ||
@Test | ||
public void closeVoteTest_alreadyComplete() { | ||
// given | ||
|
||
Long voteId = 1L; | ||
Long writerId = 1L; | ||
Long requestUserId = 1L; | ||
VoteEntity vote = | ||
VoteEntity.builder() | ||
.id(voteId) | ||
.userId(writerId) | ||
.voteEndDate(LocalDateTime.now().minusMinutes(240)) | ||
.build(); | ||
|
||
// when | ||
when(voteRepository.findById((Long) any())).thenReturn(Optional.of(vote)); | ||
|
||
// then | ||
assertThrows(CloseException.class, () -> closeVoteService.closeVote(voteId, requestUserId)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.kakao.golajuma.vote.domain.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.kakao.golajuma.vote.domain.exception.NullException; | ||
import com.kakao.golajuma.vote.infra.entity.VoteEntity; | ||
import com.kakao.golajuma.vote.infra.repository.VoteRepository; | ||
import com.kakao.golajuma.vote.web.dto.response.GetVoteDetailResponse; | ||
import com.kakao.golajuma.vote.web.dto.response.VoteDto; | ||
import java.util.Optional; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class GetVoteDetailServiceTest { | ||
@InjectMocks GetVoteDetailService getVoteDetailService; | ||
@Mock VoteRepository voteRepository; | ||
@Mock GetVoteService getVoteService; | ||
@Mock VoteEntity vote; | ||
@Mock VoteDto voteDto; | ||
|
||
@DisplayName("투표 상세 조회 정상 응답") | ||
@Test | ||
public void getVoteDetailTest() { | ||
// when | ||
when(voteRepository.findById((Long) any())).thenReturn(Optional.of(vote)); | ||
when(getVoteService.getVote(any(), any())).thenReturn(voteDto); | ||
|
||
GetVoteDetailResponse result = getVoteDetailService.getVoteDetail(1L, 1L); | ||
|
||
// then | ||
assertEquals(voteDto, result.getVote()); | ||
} | ||
|
||
@DisplayName("투표 상세 조회 예외 - 존재하지 않는 투표") | ||
@Test | ||
public void getVoteDetailTest_NotFoundVote() { | ||
// when | ||
when(voteRepository.findById((Long) any())).thenThrow(new NullException("message")); | ||
|
||
// then | ||
assertThrows(NullException.class, () -> getVoteDetailService.getVoteDetail(0L, 1L)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳