-
Notifications
You must be signed in to change notification settings - Fork 1
✨ [OMF-340] 단위 테스트 추가 - 잔여 서비스 레이어 전체 (PR 3/3) #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d679f6b
test: 잔여 서비스 레이어 단위 테스트 일괄 작성
d0f67fb
test: 잔여 서비스 레이어 단위 테스트 추가 (question, participation, formRequest)
5acd05f
refactor: 셀프 리뷰 반영 - 미사용 import 정리, verify 보완, DisplayName 개선
53fc238
test: CodeRabbit 반영 - 경계 케이스, never matcher 개선, ArgumentCaptor 적용
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
149 changes: 149 additions & 0 deletions
149
src/test/java/OneQ/OnSurvey/domain/discount/service/DiscountCodeQueryServiceTest.java
This file contains hidden or 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,149 @@ | ||
| package OneQ.OnSurvey.domain.discount.service; | ||
|
|
||
| import OneQ.OnSurvey.domain.discount.DiscountCodeErrorCode; | ||
| import OneQ.OnSurvey.domain.discount.entity.DiscountCode; | ||
| import OneQ.OnSurvey.domain.discount.model.response.DiscountCodeResponse; | ||
| import OneQ.OnSurvey.domain.discount.model.response.ValidateDiscountCodeResponse; | ||
| import OneQ.OnSurvey.domain.discount.repository.DiscountCodeRepository; | ||
| import OneQ.OnSurvey.global.common.exception.CustomException; | ||
| 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 java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.BDDMockito.given; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class DiscountCodeQueryServiceTest { | ||
|
|
||
| @Mock | ||
| private DiscountCodeRepository discountCodeRepository; | ||
|
|
||
| @InjectMocks | ||
| private DiscountCodeQueryService discountCodeQueryService; | ||
|
|
||
| private DiscountCode buildCode(String code, LocalDate expiredAt) { | ||
| return DiscountCode.of("테스트기업", code, expiredAt); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("validate - 코드 없으면 DISCOUNT_CODE_NOT_FOUND 예외") | ||
| void validate_notFound_throwsException() { | ||
| given(discountCodeRepository.findByCode("XXXXXX")).willReturn(Optional.empty()); | ||
|
|
||
| assertThatThrownBy(() -> discountCodeQueryService.validate("XXXXXX")) | ||
| .isInstanceOf(CustomException.class) | ||
| .satisfies(ex -> assertThat(((CustomException) ex).getErrorCode()) | ||
| .isEqualTo(DiscountCodeErrorCode.DISCOUNT_CODE_NOT_FOUND)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("validate - 만료된 코드면 DISCOUNT_CODE_EXPIRED 예외") | ||
| void validate_expired_throwsException() { | ||
| DiscountCode expired = buildCode("AAAAAA", LocalDate.now().minusDays(1)); | ||
| given(discountCodeRepository.findByCode("AAAAAA")).willReturn(Optional.of(expired)); | ||
|
|
||
| assertThatThrownBy(() -> discountCodeQueryService.validate("AAAAAA")) | ||
| .isInstanceOf(CustomException.class) | ||
| .satisfies(ex -> assertThat(((CustomException) ex).getErrorCode()) | ||
| .isEqualTo(DiscountCodeErrorCode.DISCOUNT_CODE_EXPIRED)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("validate - 유효한 코드면 eligible=true 반환") | ||
| void validate_valid_returnsEligibleTrue() { | ||
| DiscountCode valid = buildCode("BBBBBB", LocalDate.now().plusDays(30)); | ||
| given(discountCodeRepository.findByCode("BBBBBB")).willReturn(Optional.of(valid)); | ||
|
|
||
| ValidateDiscountCodeResponse response = discountCodeQueryService.validate("BBBBBB"); | ||
|
|
||
| assertThat(response.eligible()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getByCode - 코드 없으면 DISCOUNT_CODE_NOT_FOUND 예외") | ||
| void getByCode_notFound_throwsException() { | ||
| given(discountCodeRepository.findByCode("YYYYYY")).willReturn(Optional.empty()); | ||
|
|
||
| assertThatThrownBy(() -> discountCodeQueryService.getByCode("YYYYYY")) | ||
| .isInstanceOf(CustomException.class) | ||
| .satisfies(ex -> assertThat(((CustomException) ex).getErrorCode()) | ||
| .isEqualTo(DiscountCodeErrorCode.DISCOUNT_CODE_NOT_FOUND)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getByCode - 만료된 코드면 DISCOUNT_CODE_EXPIRED 예외") | ||
| void getByCode_expired_throwsException() { | ||
| DiscountCode expired = buildCode("CCCCCC", LocalDate.now().minusDays(1)); | ||
| given(discountCodeRepository.findByCode("CCCCCC")).willReturn(Optional.of(expired)); | ||
|
|
||
| assertThatThrownBy(() -> discountCodeQueryService.getByCode("CCCCCC")) | ||
| .isInstanceOf(CustomException.class) | ||
| .satisfies(ex -> assertThat(((CustomException) ex).getErrorCode()) | ||
| .isEqualTo(DiscountCodeErrorCode.DISCOUNT_CODE_EXPIRED)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getByCode - 유효한 코드면 엔티티 반환") | ||
| void getByCode_valid_returnsEntity() { | ||
| DiscountCode valid = buildCode("DDDDDD", LocalDate.now().plusDays(10)); | ||
| given(discountCodeRepository.findByCode("DDDDDD")).willReturn(Optional.of(valid)); | ||
|
|
||
| DiscountCode result = discountCodeQueryService.getByCode("DDDDDD"); | ||
|
|
||
| assertThat(result.getCode()).isEqualTo("DDDDDD"); | ||
| assertThat(result.getOrganizationName()).isEqualTo("테스트기업"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("findAll - 활성 코드 우선, 만료 코드 후순; 각 그룹 내 만료일 오름차순(가까운 날짜 먼저)") | ||
| void findAll_sortedActiveFirst() { | ||
| LocalDate today = LocalDate.now(); | ||
| DiscountCode expiredEarlier = buildCode("EXP001", today.minusDays(5)); | ||
| DiscountCode expiredLater = buildCode("EXP002", today.minusDays(1)); | ||
| DiscountCode activeNear = buildCode("ACT002", today.plusDays(3)); | ||
| DiscountCode activeFar = buildCode("ACT001", today.plusDays(10)); | ||
| given(discountCodeRepository.findAll()).willReturn(List.of(expiredEarlier, activeFar, expiredLater, activeNear)); | ||
|
|
||
| List<DiscountCodeResponse> results = discountCodeQueryService.findAll(); | ||
|
|
||
| assertThat(results).hasSize(4); | ||
| assertThat(results.get(0).code()).isEqualTo("ACT002"); | ||
| assertThat(results.get(1).code()).isEqualTo("ACT001"); | ||
| assertThat(results.get(2).code()).isEqualTo("EXP001"); | ||
| assertThat(results.get(3).code()).isEqualTo("EXP002"); | ||
| } | ||
|
KJaeKwan marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| @DisplayName("findAll - 만료일이 오늘인 코드는 활성으로 분류(isBefore 기준)") | ||
| void findAll_todayExpiry_treatedAsActive() { | ||
| LocalDate today = LocalDate.now(); | ||
| DiscountCode todayCode = buildCode("TODAY1", today); | ||
| DiscountCode expiredCode = buildCode("EXP001", today.minusDays(1)); | ||
| given(discountCodeRepository.findAll()).willReturn(List.of(expiredCode, todayCode)); | ||
|
|
||
| List<DiscountCodeResponse> results = discountCodeQueryService.findAll(); | ||
|
|
||
| assertThat(results).hasSize(2); | ||
| assertThat(results.get(0).code()).isEqualTo("TODAY1"); | ||
| assertThat(results.get(1).code()).isEqualTo("EXP001"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("findAll - 빈 목록이면 빈 리스트 반환") | ||
| void findAll_empty_returnsEmptyList() { | ||
| given(discountCodeRepository.findAll()).willReturn(List.of()); | ||
|
|
||
| List<DiscountCodeResponse> results = discountCodeQueryService.findAll(); | ||
|
|
||
| assertThat(results).isEmpty(); | ||
| } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
...a/OneQ/OnSurvey/domain/participation/service/answer/QuestionAnswerCommandServiceTest.java
This file contains hidden or 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,72 @@ | ||
| package OneQ.OnSurvey.domain.participation.service.answer; | ||
|
|
||
| import OneQ.OnSurvey.domain.participation.entity.QuestionAnswer; | ||
| import OneQ.OnSurvey.domain.participation.entity.Response; | ||
| import OneQ.OnSurvey.domain.participation.repository.answer.AnswerRepository; | ||
| import OneQ.OnSurvey.domain.participation.repository.response.ResponseRepository; | ||
| import OneQ.OnSurvey.domain.question.repository.question.QuestionRepository; | ||
| import OneQ.OnSurvey.global.infra.redis.RedisAgent; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class QuestionAnswerCommandServiceTest { | ||
|
|
||
| @Mock private AnswerRepository<QuestionAnswer> answerRepository; | ||
| @Mock private ResponseRepository responseRepository; | ||
| @Mock private RedisAgent redisAgent; | ||
| @Mock private QuestionRepository questionRepository; | ||
|
|
||
| @InjectMocks | ||
| private QuestionAnswerCommandService questionAnswerCommandService; | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterQuestionAnswers - 응답 없으면 신규 생성 후 저장") | ||
| void updateResponseAfterQuestionAnswers_notFound_createsAndSaves() { | ||
| given(responseRepository.findBySurveyIdAndMemberId(1L, 2L)).willReturn(Optional.empty()); | ||
|
|
||
| questionAnswerCommandService.updateResponseAfterQuestionAnswers(1L, 2L); | ||
|
|
||
| ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class); | ||
| verify(responseRepository).save(captor.capture()); | ||
| assertThat(captor.getValue().getSurveyId()).isEqualTo(1L); | ||
| assertThat(captor.getValue().getMemberId()).isEqualTo(2L); | ||
| assertThat(captor.getValue().getIsResponded()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterQuestionAnswers - 미완료 응답이면 저장") | ||
| void updateResponseAfterQuestionAnswers_notResponded_saves() { | ||
| Response response = Response.of(1L, 2L); | ||
| given(responseRepository.findBySurveyIdAndMemberId(1L, 2L)).willReturn(Optional.of(response)); | ||
|
|
||
| questionAnswerCommandService.updateResponseAfterQuestionAnswers(1L, 2L); | ||
|
|
||
| verify(responseRepository).save(response); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterQuestionAnswers - 이미 완료된 응답이면 저장 안함") | ||
| void updateResponseAfterQuestionAnswers_alreadyResponded_doesNotSave() { | ||
| Response response = Response.of(1L, 2L); | ||
| response.markResponded(); | ||
| given(responseRepository.findBySurveyIdAndMemberId(1L, 2L)).willReturn(Optional.of(response)); | ||
|
|
||
| questionAnswerCommandService.updateResponseAfterQuestionAnswers(1L, 2L); | ||
|
|
||
| verify(responseRepository, never()).save(any(Response.class)); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
.../OneQ/OnSurvey/domain/participation/service/answer/ScreeningAnswerCommandServiceTest.java
This file contains hidden or 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,102 @@ | ||
| package OneQ.OnSurvey.domain.participation.service.answer; | ||
|
|
||
| import OneQ.OnSurvey.domain.participation.entity.Response; | ||
| import OneQ.OnSurvey.domain.participation.entity.ScreeningAnswer; | ||
| import OneQ.OnSurvey.domain.participation.model.dto.AnswerInsertDto; | ||
| import OneQ.OnSurvey.domain.participation.repository.answer.AnswerRepository; | ||
| import OneQ.OnSurvey.domain.participation.repository.response.ResponseRepository; | ||
| import OneQ.OnSurvey.domain.survey.repository.screening.ScreeningRepository; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class ScreeningAnswerCommandServiceTest { | ||
|
|
||
| @Mock private AnswerRepository<ScreeningAnswer> answerRepository; | ||
| @Mock private ResponseRepository responseRepository; | ||
| @Mock private ScreeningRepository screeningRepository; | ||
|
|
||
| @InjectMocks | ||
| private ScreeningAnswerCommandService screeningAnswerCommandService; | ||
|
|
||
| private AnswerInsertDto.AnswerInfo buildAnswerInfo(Long screeningId, Long memberId, String content) { | ||
| return AnswerInsertDto.AnswerInfo.builder() | ||
| .id(screeningId) | ||
| .memberId(memberId) | ||
| .content(content) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterScreening - 기댓값과 제출값 같으면 screened=false") | ||
| void updateResponseAfterScreening_sameAsExpected_notScreened() { | ||
| given(screeningRepository.getScreeningAnswer(10L)).willReturn(true); | ||
| Response response = Response.of(5L, 1L); | ||
| given(responseRepository.findBySurveyIdAndMemberId(5L, 1L)).willReturn(Optional.of(response)); | ||
|
|
||
| AnswerInsertDto.AnswerInfo info = buildAnswerInfo(10L, 1L, "true"); | ||
| screeningAnswerCommandService.updateResponseAfterScreening(5L, info); | ||
|
|
||
| assertThat(response.getIsScreened()).isFalse(); | ||
| verify(responseRepository).save(response); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterScreening - 기댓값과 제출값 다르면 screened=true") | ||
| void updateResponseAfterScreening_differsFromExpected_screened() { | ||
| given(screeningRepository.getScreeningAnswer(10L)).willReturn(true); | ||
| Response response = Response.of(5L, 1L); | ||
| given(responseRepository.findBySurveyIdAndMemberId(5L, 1L)).willReturn(Optional.of(response)); | ||
|
|
||
| AnswerInsertDto.AnswerInfo info = buildAnswerInfo(10L, 1L, "false"); | ||
| screeningAnswerCommandService.updateResponseAfterScreening(5L, info); | ||
|
|
||
| assertThat(response.getIsScreened()).isTrue(); | ||
| verify(responseRepository).save(response); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("updateResponseAfterScreening - 기존 응답 없으면 신규 생성 후 저장") | ||
| void updateResponseAfterScreening_noExistingResponse_createsNewAndSaves() { | ||
| given(screeningRepository.getScreeningAnswer(10L)).willReturn(false); | ||
| given(responseRepository.findBySurveyIdAndMemberId(5L, 1L)).willReturn(Optional.empty()); | ||
|
|
||
| AnswerInsertDto.AnswerInfo info = buildAnswerInfo(10L, 1L, "false"); | ||
| screeningAnswerCommandService.updateResponseAfterScreening(5L, info); | ||
|
|
||
| ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class); | ||
| verify(responseRepository).save(captor.capture()); | ||
| assertThat(captor.getValue().getSurveyId()).isEqualTo(5L); | ||
| assertThat(captor.getValue().getMemberId()).isEqualTo(1L); | ||
| assertThat(captor.getValue().getIsScreened()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("insertAnswer - answerRepository.save 호출 후 updateResponseAfterScreening 진행") | ||
| void insertAnswer_savesAnswerAndUpdatesResponse() { | ||
| given(screeningRepository.getSurveyId(10L)).willReturn(5L); | ||
| given(screeningRepository.getScreeningAnswer(10L)).willReturn(true); | ||
| given(answerRepository.save(any())).willReturn(null); | ||
| Response response = Response.of(5L, 1L); | ||
| given(responseRepository.findBySurveyIdAndMemberId(5L, 1L)).willReturn(Optional.of(response)); | ||
|
|
||
| AnswerInsertDto.AnswerInfo info = buildAnswerInfo(10L, 1L, "true"); | ||
| Boolean result = screeningAnswerCommandService.insertAnswer(info); | ||
|
|
||
| assertThat(result).isTrue(); | ||
| verify(answerRepository).save(any()); | ||
| verify(responseRepository).save(response); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.