From 48e2d4156d525f7b5e672177a569486c82604b35 Mon Sep 17 00:00:00 2001 From: kseysh Date: Wed, 3 Apr 2024 00:47:09 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20FriendRepositoryTest=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=EC=97=90=20=EC=A0=80=EC=9E=A5=EB=90=98=EC=96=B4=20?= =?UTF-8?q?=EC=9E=88=EB=8A=94=20=EA=B0=92=EC=97=90=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#227)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postgres/FriendRepositoryTest.java | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/sopt/app/interfaces/postgres/FriendRepositoryTest.java b/src/test/java/org/sopt/app/interfaces/postgres/FriendRepositoryTest.java index 4be54529..73164caa 100644 --- a/src/test/java/org/sopt/app/interfaces/postgres/FriendRepositoryTest.java +++ b/src/test/java/org/sopt/app/interfaces/postgres/FriendRepositoryTest.java @@ -2,27 +2,74 @@ import java.util.List; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.sopt.app.common.config.QuerydslConfiguration; +import org.sopt.app.domain.entity.Friend; +import org.sopt.app.domain.entity.User; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; -@SpringBootTest -public class FriendRepositoryTest { +@DataJpaTest +@AutoConfigureTestDatabase(replace = Replace.NONE) +@Import(QuerydslConfiguration.class) +class FriendRepositoryTest { @Autowired private FriendRepository friendRepository; + @Autowired + private UserRepository userRepository; + + User user1 = new User(); + User user2 = new User(); + + Friend friend = new Friend(); + + @BeforeEach + void beforeTest() { + + user1 = userRepository.save( + User.builder() + .playgroundId(generateUniquePlaygroundId()) + .build() + ); + user2 = userRepository.save( + User.builder() + .playgroundId(generateUniquePlaygroundId()) + .build() + ); + + friend = friendRepository.save(Friend.builder().userId(user1.getId()).friendUserId(user2.getId()).build()); + } + + private Long generateUniquePlaygroundId() { + long playgroundId = Long.MAX_VALUE; + + for (int i = 0; i < 1000; i++) { + if (userRepository.findUserByPlaygroundId(playgroundId).isEmpty()) { + break; + } + playgroundId = playgroundId - i; + } + return playgroundId; + } + @Test - @DisplayName("200 - findAllByFriendUserId") + @DisplayName("SUCCESS_유저 아이디로 친구 찾기") void findAllByFriendUserIdSuccess() { - Assertions.assertEquals(List.of(), friendRepository.findAllByFriendUserId(1L)); + Assertions.assertEquals(List.of(friend), friendRepository.findAllByFriendUserId(user2.getId())); } @Test - @DisplayName("200 - findAllByUserIdAndFriendUserIdIn") + @DisplayName("SUCCESS_유저 아이디와 친구 아이디로 친구 찾기") void findAllByUserIdAndFriendUserIdInSuccess() { - Assertions.assertEquals(List.of(), friendRepository.findAllByUserIdAndFriendUserIdIn(1L, List.of(2L))); + Assertions.assertEquals(List.of(friend), friendRepository.findAllByUserIdAndFriendUserIdIn(user1.getId(), List.of(user2.getId()))); } } \ No newline at end of file From 44d5d5fda359275dbbcc2d8905cebd0ce7ce3fc4 Mon Sep 17 00:00:00 2001 From: kseysh Date: Wed, 3 Apr 2024 00:58:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20FriendService=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=EC=97=90?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=EB=90=98=EC=96=B4=20=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EA=B0=92=EC=97=90=20=EC=9D=98=EC=A1=B4=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=20(#227)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/application/FriendServiceTest.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/sopt/app/application/FriendServiceTest.java b/src/test/java/org/sopt/app/application/FriendServiceTest.java index 43c74ad9..9551b282 100644 --- a/src/test/java/org/sopt/app/application/FriendServiceTest.java +++ b/src/test/java/org/sopt/app/application/FriendServiceTest.java @@ -1,21 +1,37 @@ package org.sopt.app.application; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.when; + +import java.util.List; import org.junit.jupiter.api.Assertions; 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 org.sopt.app.application.poke.FriendService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.sopt.app.interfaces.postgres.FriendRepository; + +@ExtendWith(MockitoExtension.class) +class FriendServiceTest { -@SpringBootTest -public class FriendServiceTest { + @Mock + private FriendRepository friendRepository; - @Autowired + @InjectMocks private FriendService friendService; @Test - @DisplayName("200 - getIsNewUserSuccess") + @DisplayName("SUCCESS_친구가 없다면 새로운 유저로 판단하여 true 반환") void getIsNewUserSuccess() { - Assertions.assertEquals(true, friendService.getIsNewUser(1L)); + //given + final Long anyUserId = anyLong(); + //when + when(friendRepository.findAllByFriendUserId(anyUserId)).thenReturn(List.of()); + when(friendRepository.findAllByUserIdAndFriendUserIdIn(anyUserId, List.of())).thenReturn(List.of()); + //then + Assertions.assertTrue(friendService.getIsNewUser(anyUserId)); } } \ No newline at end of file From 17fe3aed67ee542924350de496847cf45cb07eb9 Mon Sep 17 00:00:00 2001 From: kseysh Date: Wed, 3 Apr 2024 01:02:35 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=B5=9C=EC=86=8C=20=ED=95=9C=20?= =?UTF-8?q?=EA=B0=9C=EC=9D=98=20Assertion=EC=9D=B4=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=20(#227)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interfaces/postgres/MainDescriptionRepositoryTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/sopt/app/interfaces/postgres/MainDescriptionRepositoryTest.java b/src/test/java/org/sopt/app/interfaces/postgres/MainDescriptionRepositoryTest.java index 72ea42e7..64f8d0c4 100644 --- a/src/test/java/org/sopt/app/interfaces/postgres/MainDescriptionRepositoryTest.java +++ b/src/test/java/org/sopt/app/interfaces/postgres/MainDescriptionRepositoryTest.java @@ -1,5 +1,6 @@ package org.sopt.app.interfaces.postgres; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.sopt.app.common.config.QuerydslConfiguration; @@ -12,7 +13,7 @@ @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) @Import(QuerydslConfiguration.class) -public class MainDescriptionRepositoryTest { +class MainDescriptionRepositoryTest { @Autowired private MainDescriptionRepository mainDescriptionRepository; @@ -20,6 +21,6 @@ public class MainDescriptionRepositoryTest { @Test @DisplayName("SUCCESS_메인 문구 전체 조회") void SUCCESS_findAll() { - System.out.println(mainDescriptionRepository.findAll()); + Assertions.assertDoesNotThrow(() -> mainDescriptionRepository.findAll()); } }