-
Couldn't load subscription status.
- Fork 1
feat: 주문 가능 상점 전환(주문 서비스 가입) POST API 검증로직 추가 및 테스트 추가 #2048
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
asa9874
merged 4 commits into
feat/2027-add-shop-to-orderable-request-api
from
feat/2038-shop-to-orderable-request-validate
Oct 27, 2025
Merged
Changes from all commits
Commits
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
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
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
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
161 changes: 161 additions & 0 deletions
161
src/test/java/in/koreatech/koin/unit/domain/ShopToOrderable/ShopToOrderableServiceTest.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,161 @@ | ||
| package in.koreatech.koin.unit.domain.ShopToOrderable; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| 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.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import in.koreatech.koin.domain.shop.model.shop.Shop; | ||
| import in.koreatech.koin.domain.shop.repository.shop.ShopRepository; | ||
| import in.koreatech.koin.domain.shoptoOrderable.dto.ShopToOrderableRequest; | ||
| import in.koreatech.koin.domain.shoptoOrderable.model.ShopToOrderable; | ||
| import in.koreatech.koin.domain.shoptoOrderable.model.ShopToOrderableRequestStatus; | ||
| import in.koreatech.koin.domain.shoptoOrderable.repository.ShopToOrderableRepository; | ||
| import in.koreatech.koin.domain.shoptoOrderable.service.ShopToOrderableService; | ||
| import in.koreatech.koin.domain.owner.model.Owner; | ||
| import in.koreatech.koin.domain.user.model.User; | ||
| import in.koreatech.koin.global.code.ApiResponseCode; | ||
| import in.koreatech.koin.global.exception.CustomException; | ||
| import in.koreatech.koin.unit.fixture.OwnerFixture; | ||
| import in.koreatech.koin.unit.fixture.ShopFixture; | ||
|
|
||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class ShopToOrderableServiceTest { | ||
|
|
||
| @InjectMocks | ||
| private ShopToOrderableService shopToOrderableService; | ||
|
|
||
| @Mock | ||
| private ShopToOrderableRepository shopToOrderableRepository; | ||
|
|
||
| @Mock | ||
| private ShopRepository shopRepository; | ||
|
|
||
| private Owner owner; | ||
| private User user; | ||
| private Shop shop; | ||
| private ShopToOrderableRequest request; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| owner = OwnerFixture.성빈_사장님(); | ||
| ReflectionTestUtils.setField(owner, "id", 1); | ||
| shop = ShopFixture.주문전환_이전_상점(owner); | ||
|
|
||
| request = new ShopToOrderableRequest( | ||
| 5000, | ||
| true, | ||
| "BOTH", | ||
| 1000, | ||
| 2000, | ||
| true, | ||
| "https://example.com/business_license.jpg", | ||
| "https://example.com/business_certificate.jpg", | ||
| "https://example.com/bank_copy.jpg", | ||
| "국민은행", | ||
| "123-456-789" | ||
| ); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("주문 가능 상점 신청 생성 테스트") | ||
| class CreateOrderableRequestTest { | ||
|
|
||
| @Test | ||
| @DisplayName("정상적으로 주문 가능 상점 신청이 생성된다") | ||
| void createOrderableRequestSuccessfully() { | ||
| // given | ||
| when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); | ||
| when(shopToOrderableRepository.existsByShopId(100)).thenReturn(false); | ||
| when(shopToOrderableRepository.existsByShopIdAndRequestStatus(100, ShopToOrderableRequestStatus.APPROVED)).thenReturn(false); | ||
| when(shopToOrderableRepository.save(any(ShopToOrderable.class))).thenAnswer( | ||
| invocation -> invocation.getArgument(0)); | ||
|
|
||
| // when | ||
| shopToOrderableService.createOrderableRequest(1, request, 100); | ||
|
|
||
| // then | ||
| ArgumentCaptor<ShopToOrderable> captor = ArgumentCaptor.forClass(ShopToOrderable.class); | ||
| verify(shopToOrderableRepository).save(captor.capture()); | ||
|
|
||
| ShopToOrderable saved = captor.getValue(); | ||
| assertThat(saved.getShop()).isEqualTo(shop); | ||
| assertThat(saved.getMinimumOrderAmount()).isEqualTo(5000); | ||
| assertThat(saved.getTakeout()).isTrue(); | ||
| assertThat(saved.getDeliveryOption()).isEqualTo("BOTH"); | ||
| assertThat(saved.getCampusDeliveryTip()).isEqualTo(1000); | ||
| assertThat(saved.getOutsideDeliveryTip()).isEqualTo(2000); | ||
| assertThat(saved.getIsOpen()).isTrue(); | ||
| assertThat(saved.getBank()).isEqualTo("국민은행"); | ||
| assertThat(saved.getAccountNumber()).isEqualTo("123-456-789"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("존재하지 않는 상점 ID로 신청하면 예외가 발생한다") | ||
| void throwExceptionWhenShopNotFound() { | ||
| // given | ||
| when(shopRepository.findById(999)).thenReturn(Optional.empty()); | ||
|
|
||
| // when & then | ||
| CustomException exception = assertThrows(CustomException.class, | ||
| () -> shopToOrderableService.createOrderableRequest(1, request, 999)); | ||
|
|
||
| assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.NOT_FOUND_SHOP); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("이미 신청한 내역이 있으면 예외가 발생한다") | ||
| void throwExceptionWhenAlreadyRequested() { | ||
| // given | ||
| when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); | ||
| when(shopToOrderableRepository.existsByShopId(100)).thenReturn(true); | ||
|
|
||
| // when & then | ||
| CustomException exception = assertThrows(CustomException.class, | ||
| () -> shopToOrderableService.createOrderableRequest(1, request, 100)); | ||
|
|
||
| assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.DUPLICATE_REQUESTED_ORDERABLE_SHOP); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("상점 사장님이 아닌 경우 예외가 발생한다") | ||
| void throwExceptionWhenNotShopOwner() { | ||
| // given | ||
| when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); | ||
|
|
||
| // when & then | ||
| CustomException exception = assertThrows(CustomException.class, | ||
| () -> shopToOrderableService.createOrderableRequest(2, request, 100)); | ||
| assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.FORBIDDEN_SHOP_OWNER); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("이미 승인된 상점이 있으면 예외가 발생한다") | ||
| void throwExceptionWhenAlreadyApproved() { | ||
| // given | ||
| when(shopRepository.findById(100)).thenReturn(Optional.of(shop)); | ||
| when(shopToOrderableRepository.existsByShopId(100)).thenReturn(false); | ||
| when(shopToOrderableRepository.existsByShopIdAndRequestStatus(100, ShopToOrderableRequestStatus.APPROVED)).thenReturn(true); | ||
|
|
||
| // when & then | ||
| CustomException exception = assertThrows(CustomException.class, | ||
| () -> shopToOrderableService.createOrderableRequest(1, request, 100)); | ||
| assertThat(exception.getErrorCode()).isEqualTo(ApiResponseCode.DUPLICATE_ORDERABLE_SHOP); | ||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
src/test/java/in/koreatech/koin/unit/fixture/ShopFixture.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,24 @@ | ||
| package in.koreatech.koin.unit.fixture; | ||
|
|
||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import in.koreatech.koin.domain.owner.model.Owner; | ||
| import in.koreatech.koin.domain.shop.model.shop.Shop; | ||
|
|
||
| public class ShopFixture { | ||
|
|
||
| private ShopFixture() {} | ||
|
|
||
| public static Shop 주문전환_이전_상점(Owner owner) { | ||
| Shop shop = Shop.builder() | ||
| .name("테스트 상점") | ||
| .owner(owner) | ||
| .phone("041-123-4567") | ||
| .address("천안시 동남구") | ||
| .description("테스트 상점입니다") | ||
| .build(); | ||
| ReflectionTestUtils.setField(shop, "id", 100); | ||
|
|
||
| return shop; | ||
| } | ||
| } |
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.
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.
리플렉션으로 id를 설정할 수도 있지만, 굳이 실제 엔티티의 내부 필드 값을 주입해야 하는 것이 아니라면
spy를 통해getId()를 스터빙 하는 것도 좋아보입니다!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.
좋은 의견 감사합니다!
ReflectionTestUtils를 사용해서private필드에 접근하는 건 저도 위험할 수 있는 행위라고 생각합니다.하지만 해당 테스트에서
Stub를 전역으로 사용하게 될 시Unnecessary stubbings detected.오류가 발생하여,각 필요한 부분을 개별적으로 정리해야 하고, 이로 인해 테스트의 주체가 아닌
Owner때문에 테스트 코드 자체의 가독성이 떨어질 수 있다고 생각해서 지금 방식으로 작성하였는데 이에 대해 의견 묻고싶습니다.Uh oh!
There was an error while loading. Please reload this page.
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.
저는 반대로 모든 테스트에서
id를 사용하지 않으니 필요한 부분에서만 사용할 수 있도록 스터빙하는 것이 맞지 않을까? 랄는 의견입니당.이 부분은 테스트 코드의 일관성을 유지하기 위해 한 가지 방법을 정해야 할 것 같아요