From a347e8a0061a6e8ea150ef0c37a9d77637db73d4 Mon Sep 17 00:00:00 2001 From: pushedrumex Date: Thu, 7 Mar 2024 12:41:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor/#60:=20PK=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=84=20Long=20=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/verby/indp/domain/contact/Contact.java | 2 +- .../java/com/verby/indp/domain/notification/Notification.java | 2 +- .../com/verby/indp/domain/recommendation/Recommendation.java | 2 +- src/main/java/com/verby/indp/domain/song/SongForm.java | 2 +- src/main/java/com/verby/indp/domain/store/Store.java | 2 +- src/main/java/com/verby/indp/domain/theme/Theme.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/verby/indp/domain/contact/Contact.java b/src/main/java/com/verby/indp/domain/contact/Contact.java index 27d4e89..e752d58 100644 --- a/src/main/java/com/verby/indp/domain/contact/Contact.java +++ b/src/main/java/com/verby/indp/domain/contact/Contact.java @@ -23,7 +23,7 @@ public class Contact extends BaseTimeEntity { @Getter @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "contact_id") - private long contactId; + private Long contactId; @Embedded private ContactUserName userName; diff --git a/src/main/java/com/verby/indp/domain/notification/Notification.java b/src/main/java/com/verby/indp/domain/notification/Notification.java index 51d9f09..ea9ca8f 100644 --- a/src/main/java/com/verby/indp/domain/notification/Notification.java +++ b/src/main/java/com/verby/indp/domain/notification/Notification.java @@ -19,7 +19,7 @@ public class Notification extends BaseTimeEntity { @Getter @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "notification_id") - private long notificationId; + private Long notificationId; @Column(name = "subject") private String subject; diff --git a/src/main/java/com/verby/indp/domain/recommendation/Recommendation.java b/src/main/java/com/verby/indp/domain/recommendation/Recommendation.java index 490111d..93f26c5 100644 --- a/src/main/java/com/verby/indp/domain/recommendation/Recommendation.java +++ b/src/main/java/com/verby/indp/domain/recommendation/Recommendation.java @@ -29,7 +29,7 @@ public class Recommendation extends BaseTimeEntity { @Getter @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "recommendation_id") - private long recommendationId; + private Long recommendationId; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "store_id") diff --git a/src/main/java/com/verby/indp/domain/song/SongForm.java b/src/main/java/com/verby/indp/domain/song/SongForm.java index b08f52c..6b98f27 100644 --- a/src/main/java/com/verby/indp/domain/song/SongForm.java +++ b/src/main/java/com/verby/indp/domain/song/SongForm.java @@ -18,7 +18,7 @@ public class SongForm { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "song_form_id") - private long songFormId; + private Long songFormId; @Embedded private SongFormName name; diff --git a/src/main/java/com/verby/indp/domain/store/Store.java b/src/main/java/com/verby/indp/domain/store/Store.java index 11dba0b..feff7ae 100644 --- a/src/main/java/com/verby/indp/domain/store/Store.java +++ b/src/main/java/com/verby/indp/domain/store/Store.java @@ -32,7 +32,7 @@ public class Store extends BaseTimeEntity { @Getter @Column(name = "store_id") @GeneratedValue(strategy = GenerationType.IDENTITY) - private long storeId; + private Long storeId; @Embedded private StoreName name; diff --git a/src/main/java/com/verby/indp/domain/theme/Theme.java b/src/main/java/com/verby/indp/domain/theme/Theme.java index 4afdd2e..5218111 100644 --- a/src/main/java/com/verby/indp/domain/theme/Theme.java +++ b/src/main/java/com/verby/indp/domain/theme/Theme.java @@ -18,7 +18,7 @@ public class Theme { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "theme_id") - private long themeId; + private Long themeId; @Embedded private ThemeName name; From e6aef44aa695cab0aacc1c9d5d96736bc5df250c Mon Sep 17 00:00:00 2001 From: pushedrumex Date: Thu, 7 Mar 2024 12:41:27 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor/#60:=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ContactControllerTest.java | 3 +- .../contact/service/ContactServiceTest.java | 2 ++ .../RecommendationControllerTest.java | 5 +++- .../service/RecommendationServiceTest.java | 10 ++++--- .../store/controller/StoreControllerTest.java | 6 ++-- .../domain/store/fixture/StoreFixture.java | 30 +++++++++++++++++++ .../store/service/StoreServiceTest.java | 10 +++---- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/verby/indp/domain/contact/controller/ContactControllerTest.java b/src/test/java/com/verby/indp/domain/contact/controller/ContactControllerTest.java index d7cc19e..4c3bd35 100644 --- a/src/test/java/com/verby/indp/domain/contact/controller/ContactControllerTest.java +++ b/src/test/java/com/verby/indp/domain/contact/controller/ContactControllerTest.java @@ -1,6 +1,7 @@ package com.verby.indp.domain.contact.controller; import static com.verby.indp.domain.contact.fixture.ContactFixture.contact; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.when; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; @@ -29,7 +30,7 @@ void registerContact() throws Exception { RegisterContactRequest request = new RegisterContactRequest(contact.getUserName(), contact.getContent(), contact.getPhoneNumber()); - when(contactService.registerContact(request)).thenReturn(contact.getContactId()); + when(contactService.registerContact(request)).thenReturn(anyLong()); // when ResultActions resultActions = mockMvc.perform( diff --git a/src/test/java/com/verby/indp/domain/contact/service/ContactServiceTest.java b/src/test/java/com/verby/indp/domain/contact/service/ContactServiceTest.java index c13a1d0..02d1d8f 100644 --- a/src/test/java/com/verby/indp/domain/contact/service/ContactServiceTest.java +++ b/src/test/java/com/verby/indp/domain/contact/service/ContactServiceTest.java @@ -18,6 +18,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.test.util.ReflectionTestUtils; @ExtendWith(MockitoExtension.class) class ContactServiceTest { @@ -40,6 +41,7 @@ class RegisterContact { void registerContact() { // given Contact contact = contact(); + ReflectionTestUtils.setField(contact, "contactId", 1L); RegisterContactRequest request = new RegisterContactRequest(contact.getUserName(), contact.getContent(), contact.getPhoneNumber()); diff --git a/src/test/java/com/verby/indp/domain/recommendation/controller/RecommendationControllerTest.java b/src/test/java/com/verby/indp/domain/recommendation/controller/RecommendationControllerTest.java index 30eefb4..cf56037 100644 --- a/src/test/java/com/verby/indp/domain/recommendation/controller/RecommendationControllerTest.java +++ b/src/test/java/com/verby/indp/domain/recommendation/controller/RecommendationControllerTest.java @@ -2,6 +2,7 @@ import static com.verby.indp.domain.recommendation.fixture.RecommendationFixture.recommendation; import static com.verby.indp.domain.store.fixture.StoreFixture.store; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.when; import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders; @@ -19,6 +20,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.http.MediaType; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.ResultActions; class RecommendationControllerTest extends BaseControllerTest { @@ -28,12 +30,13 @@ class RecommendationControllerTest extends BaseControllerTest { void registerRecommendation() throws Exception { // given Store store = store(); + ReflectionTestUtils.setField(store, "storeId", 1L); Recommendation recommendation = recommendation(store()); RegisterRecommendationRequest request = new RegisterRecommendationRequest(store.getStoreId(), recommendation.getInformation(), recommendation.getPhoneNumber()); - when(recommendationService.registerRecommendation(request)).thenReturn(recommendation.getRecommendationId()); + when(recommendationService.registerRecommendation(request)).thenReturn(anyLong()); // when ResultActions resultActions = mockMvc.perform( diff --git a/src/test/java/com/verby/indp/domain/recommendation/service/RecommendationServiceTest.java b/src/test/java/com/verby/indp/domain/recommendation/service/RecommendationServiceTest.java index dffee73..ecfe36b 100644 --- a/src/test/java/com/verby/indp/domain/recommendation/service/RecommendationServiceTest.java +++ b/src/test/java/com/verby/indp/domain/recommendation/service/RecommendationServiceTest.java @@ -26,6 +26,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.test.util.ReflectionTestUtils; @ExtendWith(MockitoExtension.class) class RecommendationServiceTest { @@ -50,14 +51,15 @@ class RegisterRecommendation { @DisplayName("성공: 추천 음악 정보를 저장한다.") void registerRecommendation() { // given - long storeId = 1L; Store store = store(); + ReflectionTestUtils.setField(store, "storeId", 1L); Recommendation recommendation = recommendation(store); - - RegisterRecommendationRequest request = new RegisterRecommendationRequest(storeId, + ReflectionTestUtils.setField(recommendation, "recommendationId", 1L); + + RegisterRecommendationRequest request = new RegisterRecommendationRequest(store.getStoreId(), recommendation.getInformation(), recommendation.getPhoneNumber()); - when(storeRepository.findById(anyLong())).thenReturn(Optional.of(store)); + when(storeRepository.findById(store.getStoreId())).thenReturn(Optional.of(store)); when(recommendationRepository.save(any(Recommendation.class))).thenReturn( recommendation); diff --git a/src/test/java/com/verby/indp/domain/store/controller/StoreControllerTest.java b/src/test/java/com/verby/indp/domain/store/controller/StoreControllerTest.java index 731533e..03a34fb 100644 --- a/src/test/java/com/verby/indp/domain/store/controller/StoreControllerTest.java +++ b/src/test/java/com/verby/indp/domain/store/controller/StoreControllerTest.java @@ -1,7 +1,7 @@ package com.verby.indp.domain.store.controller; import static com.verby.indp.domain.store.constant.Region.서울; -import static com.verby.indp.domain.store.fixture.StoreFixture.stores; +import static com.verby.indp.domain.store.fixture.StoreFixture.storesWithId; import static org.mockito.Mockito.when; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.payload.JsonFieldType.ARRAY; @@ -41,7 +41,7 @@ void findSimpleStores() throws Exception { int page = 0; int size = 2; - List stores = stores(List.of(), List.of(), count); + List stores = storesWithId(List.of(), List.of(), count); Pageable pageable = PageRequest.of(page, size); Page pageStores = new PageImpl<>(stores.subList(page * size, size), pageable, count); @@ -88,7 +88,7 @@ void findStores() throws Exception { int page = 0; int size = 2; - List stores = stores(List.of(), List.of(), count, 서울); + List stores = storesWithId(List.of(), List.of(), count, 서울); Pageable pageable = PageRequest.of(page, size); Page pageStores = new PageImpl<>(stores.subList(page * size, size), pageable, count); diff --git a/src/test/java/com/verby/indp/domain/store/fixture/StoreFixture.java b/src/test/java/com/verby/indp/domain/store/fixture/StoreFixture.java index 4049a05..ae4bb79 100644 --- a/src/test/java/com/verby/indp/domain/store/fixture/StoreFixture.java +++ b/src/test/java/com/verby/indp/domain/store/fixture/StoreFixture.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.springframework.test.util.ReflectionTestUtils; public class StoreFixture { @@ -66,6 +67,20 @@ public static List stores( .collect(Collectors.toCollection(ArrayList::new)); } + public static List storesWithId( + List themes, + List songForms, + int count + ) { + return IntStream.range(0, count) + .mapToObj(i -> { + Store store = store(themes, songForms); + ReflectionTestUtils.setField(store, "storeId", (long) i); + return store; + }) + .collect(Collectors.toCollection(ArrayList::new)); + } + public static List stores( List themes, List songForms, @@ -77,4 +92,19 @@ public static List stores( .collect(Collectors.toCollection(ArrayList::new)); } + public static List storesWithId( + List themes, + List songForms, + int count, + Region region + ) { + return IntStream.range(0, count) + .mapToObj(i -> { + Store store = store(themes, songForms, region); + ReflectionTestUtils.setField(store, "storeId", (long) i); + return store; + }) + .collect(Collectors.toCollection(ArrayList::new)); + } + } diff --git a/src/test/java/com/verby/indp/domain/store/service/StoreServiceTest.java b/src/test/java/com/verby/indp/domain/store/service/StoreServiceTest.java index e2105ae..b62145f 100644 --- a/src/test/java/com/verby/indp/domain/store/service/StoreServiceTest.java +++ b/src/test/java/com/verby/indp/domain/store/service/StoreServiceTest.java @@ -3,7 +3,7 @@ import static com.verby.indp.domain.store.constant.Region.경기; import static com.verby.indp.domain.store.constant.Region.서울; -import static com.verby.indp.domain.store.fixture.StoreFixture.stores; +import static com.verby.indp.domain.store.fixture.StoreFixture.storesWithId; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -47,7 +47,7 @@ void findSimpleStores() { int page = 0; int size = 10; - List stores = stores(List.of(), List.of(), count); + List stores = storesWithId(List.of(), List.of(), count); Pageable pageable = PageRequest.of(page, size); Page pageStores = new PageImpl<>(stores.subList(page, size), pageable, count); @@ -81,7 +81,7 @@ void findStoresOfRegion() { int page = 0; int size = 10; - List seoulStores = stores(List.of(), List.of(), seoulCount, 서울); + List seoulStores = storesWithId(List.of(), List.of(), seoulCount, 서울); Pageable pageable = PageRequest.of(page, size); Page pageStores = new PageImpl<>( seoulStores.subList(page, Math.min(size, seoulCount)), pageable, seoulCount); @@ -113,8 +113,8 @@ void findStores() { int page = 0; int size = 10; - List seoulStores = stores(List.of(), List.of(), seoulCount, 서울); - List gyeonggiStores = stores(List.of(), List.of(), gyeonggiCount, 경기); + List seoulStores = storesWithId(List.of(), List.of(), seoulCount, 서울); + List gyeonggiStores = storesWithId(List.of(), List.of(), gyeonggiCount, 경기); List allStores = new ArrayList<>(); allStores.addAll(seoulStores); allStores.addAll(gyeonggiStores);