From 96318df1f0af72575171273731fdf28dccb3ded2 Mon Sep 17 00:00:00 2001 From: CHAE Date: Sat, 14 Oct 2023 11:33:43 +0900 Subject: [PATCH 1/4] =?UTF-8?q?:bug:=20Fix:=20User=20image=20=EC=86=8D?= =?UTF-8?q?=EC=84=B1=20=EB=88=84=EB=9D=BD=20=EC=88=98=EC=A0=95=20(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diareat/diareat/user/domain/User.java | 3 ++- .../diareat/user/dto/CreateUserDto.java | 5 +++-- .../diareat/user/service/UserService.java | 5 +++-- .../diareat/service/FoodServiceTest.java | 20 +++++++++---------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/domain/User.java b/src/main/java/com/diareat/diareat/user/domain/User.java index 2f0d619..47d470e 100644 --- a/src/main/java/com/diareat/diareat/user/domain/User.java +++ b/src/main/java/com/diareat/diareat/user/domain/User.java @@ -44,9 +44,10 @@ public class User { private List favoriteFoods = new ArrayList<>(); // 생성 메서드 - public static User createUser(String name, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { + public static User createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { User user = new User(); user.name = name; + user.image = image; user.keyCode = keyCode; user.height = height; user.weight = weight; diff --git a/src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java b/src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java index 481bef4..5c463ef 100644 --- a/src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java @@ -10,13 +10,14 @@ public class CreateUserDto { private String name; + private String image; private String keyCode; private int gender; private int height; private int weight; private int age; - public static CreateUserDto of(String name, String keyCode, int gender, int height, int weight, int age){ - return new CreateUserDto(name, keyCode, gender, height, weight, age); + public static CreateUserDto of(String name, String image, String keyCode, int gender, int height, int weight, int age){ + return new CreateUserDto(name, image, keyCode, gender, height, weight, age); } } diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index fe49358..caf312a 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -29,7 +29,7 @@ public Long saveUser(CreateUserDto createUserDto) { // BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight()); if (userRepository.existsByName(createUserDto.getName())) throw new UserException(ResponseCode.USER_ALREADY_EXIST); - User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); + User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); return userRepository.save(user).getId(); } @@ -68,6 +68,7 @@ public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) { User user = getUserById(updateUserNutritionDto.getUserId()); BaseNutrition baseNutrition = BaseNutrition.createNutrition(updateUserNutritionDto.getCalorie(), updateUserNutritionDto.getCarbohydrate(), updateUserNutritionDto.getProtein(), updateUserNutritionDto.getFat()); user.updateBaseNutrition(baseNutrition); + userRepository.save(user); } // 회원 탈퇴 @@ -103,7 +104,7 @@ public void unfollowUser(Long userId, Long unfollowId) { validateUser(userId); validateUser(unfollowId); // 이미 팔로우 취소한 경우 - if (followRepository.existsByFromUserAndToUser(userId, unfollowId)) + if (!followRepository.existsByFromUserAndToUser(userId, unfollowId)) throw new UserException(ResponseCode.UNFOLLOWED_ALREADY); followRepository.delete(Follow.makeFollow(userId, unfollowId)); } diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 904899c..759ecbb 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -53,7 +53,7 @@ public void setUp() { void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기 // given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1,180, 80,18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1,180, 80,18)); //when Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition)); @@ -69,7 +69,7 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리 void testUpdateFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); //when @@ -90,7 +90,7 @@ void testUpdateFood() { void testDeleteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser","testImage", "tessPassword", 1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); //when @@ -103,7 +103,7 @@ void testDeleteFood() { void testSaveAndGetFavoriteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); //when @@ -119,7 +119,7 @@ void testSaveAndGetFavoriteFood() { void testUpdateFavoriteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); @@ -142,7 +142,7 @@ void testUpdateFavoriteFood() { void testDeleteFavoriteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); @@ -156,7 +156,7 @@ void testDeleteFavoriteFood() { void testNutritionSumByDate(){ //given BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword",1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword",1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition)); Food food = foodRepository.getReferenceById(foodId); @@ -177,7 +177,7 @@ void testNutritionSumByDate(){ void testNutritionSumByWeekAndMonth(){ //given BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword",1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword",1, 180, 80, 18)); Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition)); } @@ -185,7 +185,7 @@ void testNutritionSumByWeekAndMonth(){ @Test void getBest3FoodTest() { // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1, 180, 80, 18)); foodService.saveFood(CreateFoodDto.of(userId, "Food1", BaseNutrition.createNutrition(100, 100 ,10, 1))); foodService.saveFood(CreateFoodDto.of(userId, "Food2", BaseNutrition.createNutrition(100, 100 ,8, 2))); foodService.saveFood(CreateFoodDto.of(userId, "Food3", BaseNutrition.createNutrition(100, 100 ,6, 3))); @@ -208,7 +208,7 @@ void getBest3FoodTest() { @Test void getWorst3FoodsTest() { // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1, 180, 80, 18)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1, 180, 80, 18)); foodService.saveFood(CreateFoodDto.of(userId, "Food1", BaseNutrition.createNutrition(100, 50 ,10, 1))); foodService.saveFood(CreateFoodDto.of(userId, "Food2", BaseNutrition.createNutrition(100, 100 ,8, 20))); foodService.saveFood(CreateFoodDto.of(userId, "Food3", BaseNutrition.createNutrition(100, 80 ,6, 7))); From 0773ebf759dafa2f5e3952d662cb105f173b6877 Mon Sep 17 00:00:00 2001 From: CHAE Date: Sat, 14 Oct 2023 11:34:47 +0900 Subject: [PATCH 2/4] =?UTF-8?q?:white=5Fcheck=5Fmark:=20Refactor:=20UserSe?= =?UTF-8?q?rviceTest=20Mock=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81=20(=EA=B2=80=EC=83=89=20=EC=A0=9C=EC=99=B8)?= =?UTF-8?q?=20(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/service/UserServiceTest.java | 211 ++++++++++-------- 1 file changed, 118 insertions(+), 93 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index 7d2ec45..329e8be 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -1,167 +1,192 @@ package com.diareat.diareat.service; +import com.diareat.diareat.user.domain.BaseNutrition; +import com.diareat.diareat.user.domain.Follow; +import com.diareat.diareat.user.domain.User; import com.diareat.diareat.user.dto.*; import com.diareat.diareat.user.repository.FollowRepository; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.user.service.UserService; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.transaction.annotation.Transactional; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; -@SpringBootTest -@Transactional +@ExtendWith(MockitoExtension.class) class UserServiceTest { - @Autowired - UserService userService; - - @Autowired - UserRepository userRepository; + @InjectMocks + private UserService userService; - @Autowired - FollowRepository followRepository; - - @BeforeEach - void setUp() { - userRepository.deleteAll(); - followRepository.deleteAll(); - } + @Mock + private UserRepository userRepository; - @AfterEach - void tearDown() { - userRepository.deleteAll(); - followRepository.deleteAll(); - } + @Mock + private FollowRepository followRepository; + @DisplayName("회원정보 저장") @Test void saveUser() { - // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + // Given + CreateUserDto createUserDto = CreateUserDto.of("test", "profile.jpg", "testPassword", 0, 75, 1, 25); + BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); + User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); - // when - ResponseUserDto responseUserDto = userService.getUserInfo(userId); + given(userRepository.existsByName("test")).willReturn(false); // 유저가 존재하지 않음 + given(userRepository.save(any(User.class))).willReturn(user); + + // When + Long id = userService.saveUser(createUserDto); - // then - assertNotNull(responseUserDto); - assertEquals("testUser", responseUserDto.getName()); - assertEquals(25, responseUserDto.getAge()); + // Then + assertEquals(user.getId(), id); // 저장된 사용자의 ID와 반환된 ID를 비교 + verify(userRepository, times(1)).save(any(User.class)); // userRepository의 save 메서드가 1번 호출되었는지 확인 } @Test void getSimpleUserInfo() { - // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + // Given + Long userId = 1L; + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + given(userRepository.findById(userId)).willReturn(Optional.of(user)); - // when - ResponseSimpleUserDto responseSimpleUserDto = userService.getSimpleUserInfo(userId); + // When + ResponseSimpleUserDto result = userService.getSimpleUserInfo(userId); - // then - assertEquals("testUser", responseSimpleUserDto.getName()); + // Then + assertEquals("test", result.getName()); + assertEquals("profile.jpg", result.getImage()); } @Test void getUserInfo() { - // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + // Given + Long userId = 1L; + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + given(userRepository.findById(userId)).willReturn(Optional.of(user)); - // when - ResponseUserDto responseUserDto = userService.getUserInfo(userId); + // When + ResponseUserDto result = userService.getUserInfo(userId); - // then - assertEquals("testUser", responseUserDto.getName()); + // Then + assertEquals("test", result.getName()); + assertEquals(30, result.getAge()); } @Test void updateUserInfo() { // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); - UpdateUserDto updateUserDto = UpdateUserDto.of(userId, "updateUser", 180, 75, 25, false); + UpdateUserDto updateUserDto = UpdateUserDto.of(1L, "update", 180, 75, 25, false); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + given(userRepository.findById(updateUserDto.getUserId())).willReturn(Optional.of(user)); // when userService.updateUserInfo(updateUserDto); - // then - assertEquals("updateUser", userService.getUserInfo(userId).getName()); - assertEquals(180, userService.getUserInfo(userId).getHeight()); + + // Then + assertEquals("update", user.getName()); + assertEquals(180, user.getHeight()); + assertEquals(75, user.getWeight()); + assertEquals(25, user.getAge()); } @Test void getUserNutrition() { // 임시 코드 사용, 추후 로직 개편 시 테스트코드 수정 - // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); - - // when - ResponseUserNutritionDto responseUserNutritionDto = userService.getUserNutrition(userId); - - // then - assertEquals(2000, responseUserNutritionDto.getCalorie()); + // Given + Long userId = 1L; + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + given(userRepository.findById(userId)).willReturn(Optional.of(user)); + + // When + ResponseUserNutritionDto result = userService.getUserNutrition(userId); + + // Then + assertEquals(2000, result.getCalorie()); + assertEquals(300, result.getCarbohydrate()); + assertEquals(80, result.getProtein()); + assertEquals(80, result.getFat()); } @Test void updateBaseNutrition() { - // given - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); + // Given + UpdateUserNutritionDto updateUserNutritionDto = UpdateUserNutritionDto.of(1L, 2000, 300, 80, 80); + // 필드 초기화 + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(1000, 100, 40, 40)); + given(userRepository.findById(updateUserNutritionDto.getUserId())).willReturn(Optional.of(user)); - // when - UpdateUserNutritionDto updateUserNutritionDto = UpdateUserNutritionDto.of(userId, 2000, 300, 80, 80); + // When userService.updateBaseNutrition(updateUserNutritionDto); - // then - assertEquals(2000, userService.getUserNutrition(userId).getCalorie()); + // Then + assertEquals(2000, user.getBaseNutrition().getKcal()); + assertEquals(300, user.getBaseNutrition().getCarbohydrate()); + assertEquals(80, user.getBaseNutrition().getProtein()); + assertEquals(80, user.getBaseNutrition().getFat()); } @Test void deleteUser() { - // given - Long id = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + // Given + Long userId = 1L; + given(userRepository.existsById(userId)).willReturn(true); - // when - userService.deleteUser(id); + // When + userService.deleteUser(userId); - // then - assertFalse(userRepository.existsById(id)); + // Then + verify(userRepository, times(1)).deleteById(userId); } @Test void searchUser() { - // given - userService.saveUser(CreateUserDto.of("user1", "testPassword", 0, 175, 80, 25)); - Long id = userService.saveUser(CreateUserDto.of("user2", "testPassword", 0, 175, 80, 25)); - String name = "user"; - // then - assertEquals(2, userService.searchUser(id, name).size()); } @Test void followUser() { - // given - Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); - Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 0, 75, 1, 25)); - - // when - userService.followUser(id2, id1); - - // then - assertEquals(1, followRepository.findAllByFromUser(id1).size()); + // Given + Long userId = 1L; // 팔로우 요청을 보낸 사용자의 ID + Long followId = 2L; // 팔로우할 사용자의 ID + + given(userRepository.existsById(userId)).willReturn(true); // userId에 해당하는 사용자가 존재함 + given(userRepository.existsById(followId)).willReturn(true); // followId에 해당하는 사용자가 존재함 + given(followRepository.existsByFromUserAndToUser(userId, followId)).willReturn(false); // 아직 팔로우 중이 아님 + + // When + userService.followUser(userId, followId); + + // Then + verify(userRepository, times(1)).existsById(userId); // 사용자 존재 여부 확인 + verify(userRepository, times(1)).existsById(followId); // 팔로우할 사용자 존재 여부 확인 + verify(followRepository, times(1)).existsByFromUserAndToUser(userId, followId); // 이미 팔로우 중인지 확인 + verify(followRepository, times(1)).save(any(Follow.class)); } @Test void unfollowUser() { - // given - Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 175, 1, 25)); - Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 0, 175, 1, 25)); + // Given + Long userId = 1L; // 팔로우 취소를 요청한 사용자의 ID + Long unfollowId = 2L; // 팔로우를 취소할 사용자의 ID - // when - userService.followUser(id1, id2); - userService.unfollowUser(id1, id2); + given(userRepository.existsById(userId)).willReturn(true); // userId에 해당하는 사용자가 존재함 + given(userRepository.existsById(unfollowId)).willReturn(true); // unfollowId에 해당하는 사용자가 존재함 + given(followRepository.existsByFromUserAndToUser(userId, unfollowId)).willReturn(true); + + // When + userService.unfollowUser(userId, unfollowId); - // then - assertEquals(0, followRepository.findAllByFromUser(id1).size()); + // Then + verify(followRepository, times(1)).delete(any(Follow.class)); } } \ No newline at end of file From edb2303f71713160c07b61ca460ca399dd387eba Mon Sep 17 00:00:00 2001 From: CHAE Date: Sun, 15 Oct 2023 01:15:49 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:bug:=20Fix:=20UserController=20import=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC=20=EB=B0=8F=20UserService=20=EA=B2=80?= =?UTF-8?q?=EC=83=89=EA=B8=B0=EB=8A=A5=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#26)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 1 - .../com/diareat/diareat/user/domain/User.java | 8 +-- .../diareat/user/service/UserService.java | 4 +- .../diareat/service/UserServiceTest.java | 60 ++++++++++++++----- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/controller/UserController.java b/src/main/java/com/diareat/diareat/user/controller/UserController.java index ce6c3f5..5024b92 100644 --- a/src/main/java/com/diareat/diareat/user/controller/UserController.java +++ b/src/main/java/com/diareat/diareat/user/controller/UserController.java @@ -7,7 +7,6 @@ import io.swagger.annotations.Api; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; -import net.bytebuddy.implementation.bind.annotation.Empty; import org.springframework.web.bind.annotation.*; import java.util.List; diff --git a/src/main/java/com/diareat/diareat/user/domain/User.java b/src/main/java/com/diareat/diareat/user/domain/User.java index 47d470e..178cb50 100644 --- a/src/main/java/com/diareat/diareat/user/domain/User.java +++ b/src/main/java/com/diareat/diareat/user/domain/User.java @@ -14,11 +14,11 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -public class User { +public class TestUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") - private Long id; + protected Long id; private String name; // 닉네임 @@ -44,8 +44,8 @@ public class User { private List favoriteFoods = new ArrayList<>(); // 생성 메서드 - public static User createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { - User user = new User(); + public static TestUser createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { + TestUser user = new TestUser(); user.name = name; user.image = image; user.keyCode = keyCode; diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index caf312a..80db78a 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -82,7 +83,8 @@ public void deleteUser(Long userId) { @Transactional(readOnly = true) public List searchUser(Long hostId, String name) { validateUser(hostId); - List users = userRepository.findAllByNameContaining(name); + List users = new ArrayList<>(userRepository.findAllByNameContaining(name)); + users.removeIf(user -> user.getId().equals(hostId)); // 검색 결과에서 자기 자신은 제외 (removeIf 메서드는 ArrayList에만 존재) return users.stream() .map(user -> ResponseSearchUserDto.of(user.getId(), user.getName(), user.getImage(), followRepository.existsByFromUserAndToUser(hostId, user.getId()))).collect(Collectors.toList()); } diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index 329e8be..fc2c222 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -2,7 +2,7 @@ import com.diareat.diareat.user.domain.BaseNutrition; import com.diareat.diareat.user.domain.Follow; -import com.diareat.diareat.user.domain.User; +import com.diareat.diareat.user.domain.TestUser; import com.diareat.diareat.user.dto.*; import com.diareat.diareat.user.repository.FollowRepository; import com.diareat.diareat.user.repository.UserRepository; @@ -14,6 +14,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; @@ -22,7 +23,7 @@ import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) -class UserServiceTest { +class TestUserServiceTest { @InjectMocks private UserService userService; @@ -35,28 +36,29 @@ class UserServiceTest { @DisplayName("회원정보 저장") @Test - void saveUser() { + void saveUser() { // setId() 메서드 임시 삽입하여 테스트 진행함 // Given CreateUserDto createUserDto = CreateUserDto.of("test", "profile.jpg", "testPassword", 0, 75, 1, 25); BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); - User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); + TestUser user = TestUser.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); + user.setId(1L); // 테스트 커밋 중 User에 setId() 메서드 임시적으로 삽입하여 테스트 진행함 - given(userRepository.existsByName("test")).willReturn(false); // 유저가 존재하지 않음 - given(userRepository.save(any(User.class))).willReturn(user); + given(userRepository.existsByName("test")).willReturn(false); + given(userRepository.save(any(TestUser.class))).willReturn(user); // When - Long id = userService.saveUser(createUserDto); + Long id = userService.saveUser(createUserDto); // id null로 반환됨 (Mock은 실제 DB에 객체를 생성하지 않기 때문) // Then - assertEquals(user.getId(), id); // 저장된 사용자의 ID와 반환된 ID를 비교 - verify(userRepository, times(1)).save(any(User.class)); // userRepository의 save 메서드가 1번 호출되었는지 확인 + assertEquals(1L, id); + verify(userRepository, times(1)).save(any(TestUser.class)); } @Test void getSimpleUserInfo() { // Given Long userId = 1L; - User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -71,7 +73,7 @@ void getSimpleUserInfo() { void getUserInfo() { // Given Long userId = 1L; - User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -86,7 +88,7 @@ void getUserInfo() { void updateUserInfo() { // given UpdateUserDto updateUserDto = UpdateUserDto.of(1L, "update", 180, 75, 25, false); - User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(updateUserDto.getUserId())).willReturn(Optional.of(user)); // when @@ -104,7 +106,7 @@ void updateUserInfo() { void getUserNutrition() { // 임시 코드 사용, 추후 로직 개편 시 테스트코드 수정 // Given Long userId = 1L; - User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -122,7 +124,7 @@ void updateBaseNutrition() { // Given UpdateUserNutritionDto updateUserNutritionDto = UpdateUserNutritionDto.of(1L, 2000, 300, 80, 80); // 필드 초기화 - User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(1000, 100, 40, 40)); + TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(1000, 100, 40, 40)); given(userRepository.findById(updateUserNutritionDto.getUserId())).willReturn(Optional.of(user)); // When @@ -149,8 +151,36 @@ void deleteUser() { } @Test - void searchUser() { + void searchUser() { // setId() 메서드 임시 삽입하여 테스트 진행함 + // Given + Long userId1 = 1L; + Long userId2 = 2L; + Long userId3 = 3L; + String name = "John"; + + // 사용자 목록 생성 + TestUser user1 = TestUser.createUser("John", "profile1.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user2 = TestUser.createUser("John Doe", "profile2.jpg", "keycode456", 170, 65, 1, 35, BaseNutrition.createNutrition(2000, 300, 80, 80)); + TestUser user3 = TestUser.createUser("John Doo", "profile3.jpg", "keycode789", 160, 55, 1, 25, BaseNutrition.createNutrition(2000, 300, 80, 80)); + user1.setId(userId1); + user2.setId(userId2); + user3.setId(userId3); + + given(userRepository.existsById(userId1)).willReturn(true); + given(userRepository.findAllByNameContaining(name)).willReturn(List.of(user1, user2, user3)); + given(followRepository.existsByFromUserAndToUser(userId1, userId2)).willReturn(true); + given(followRepository.existsByFromUserAndToUser(userId1, userId3)).willReturn(false); + + // When + List result = userService.searchUser(userId1, name); + // Then + assertEquals(2, result.size()); + assertEquals("John Doe", result.get(0).getName()); + assertTrue(result.get(0).isFollow()); + assertEquals("John Doo", result.get(1).getName()); + assertFalse(result.get(1).isFollow()); + verify(userRepository, times(1)).findAllByNameContaining(name); } @Test From 2e8afd8a0dffca9a875603e8b1935766c96a8b43 Mon Sep 17 00:00:00 2001 From: CHAE Date: Sun, 15 Oct 2023 01:18:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:recycle:=20Refactor:=20UserServiceTest=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=EA=B8=B0=EB=8A=A5=20Mock=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=8F=20User=EC=97=90=20SetId()=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diareat/diareat/user/domain/User.java | 12 +++++--- .../diareat/service/UserServiceTest.java | 30 +++++++++---------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/domain/User.java b/src/main/java/com/diareat/diareat/user/domain/User.java index 178cb50..6852f48 100644 --- a/src/main/java/com/diareat/diareat/user/domain/User.java +++ b/src/main/java/com/diareat/diareat/user/domain/User.java @@ -14,11 +14,11 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -public class TestUser { +public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") - protected Long id; + private Long id; private String name; // 닉네임 @@ -44,8 +44,8 @@ public class TestUser { private List favoriteFoods = new ArrayList<>(); // 생성 메서드 - public static TestUser createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { - TestUser user = new TestUser(); + public static User createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { + User user = new User(); user.name = name; user.image = image; user.keyCode = keyCode; @@ -69,4 +69,8 @@ public void updateUser(String name, int height, int weight, int age) { public void updateBaseNutrition(BaseNutrition baseNutrition) { this.baseNutrition = baseNutrition; } + + public void setId(Long id) { // 테스트코드용 메서드 + this.id = id; + } } diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index fc2c222..0d062e9 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -2,7 +2,7 @@ import com.diareat.diareat.user.domain.BaseNutrition; import com.diareat.diareat.user.domain.Follow; -import com.diareat.diareat.user.domain.TestUser; +import com.diareat.diareat.user.domain.User; import com.diareat.diareat.user.dto.*; import com.diareat.diareat.user.repository.FollowRepository; import com.diareat.diareat.user.repository.UserRepository; @@ -23,7 +23,7 @@ import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) -class TestUserServiceTest { +class UserServiceTest { @InjectMocks private UserService userService; @@ -36,29 +36,29 @@ class TestUserServiceTest { @DisplayName("회원정보 저장") @Test - void saveUser() { // setId() 메서드 임시 삽입하여 테스트 진행함 + void saveUser() { // setId() 메서드로 테스트 진행함 // Given CreateUserDto createUserDto = CreateUserDto.of("test", "profile.jpg", "testPassword", 0, 75, 1, 25); BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); - TestUser user = TestUser.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); + User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); user.setId(1L); // 테스트 커밋 중 User에 setId() 메서드 임시적으로 삽입하여 테스트 진행함 given(userRepository.existsByName("test")).willReturn(false); - given(userRepository.save(any(TestUser.class))).willReturn(user); + given(userRepository.save(any(User.class))).willReturn(user); // When Long id = userService.saveUser(createUserDto); // id null로 반환됨 (Mock은 실제 DB에 객체를 생성하지 않기 때문) // Then assertEquals(1L, id); - verify(userRepository, times(1)).save(any(TestUser.class)); + verify(userRepository, times(1)).save(any(User.class)); } @Test void getSimpleUserInfo() { // Given Long userId = 1L; - TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -73,7 +73,7 @@ void getSimpleUserInfo() { void getUserInfo() { // Given Long userId = 1L; - TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -88,7 +88,7 @@ void getUserInfo() { void updateUserInfo() { // given UpdateUserDto updateUserDto = UpdateUserDto.of(1L, "update", 180, 75, 25, false); - TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(updateUserDto.getUserId())).willReturn(Optional.of(user)); // when @@ -106,7 +106,7 @@ void updateUserInfo() { void getUserNutrition() { // 임시 코드 사용, 추후 로직 개편 시 테스트코드 수정 // Given Long userId = 1L; - TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); given(userRepository.findById(userId)).willReturn(Optional.of(user)); // When @@ -124,7 +124,7 @@ void updateBaseNutrition() { // Given UpdateUserNutritionDto updateUserNutritionDto = UpdateUserNutritionDto.of(1L, 2000, 300, 80, 80); // 필드 초기화 - TestUser user = TestUser.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(1000, 100, 40, 40)); + User user = User.createUser("test", "profile.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(1000, 100, 40, 40)); given(userRepository.findById(updateUserNutritionDto.getUserId())).willReturn(Optional.of(user)); // When @@ -151,7 +151,7 @@ void deleteUser() { } @Test - void searchUser() { // setId() 메서드 임시 삽입하여 테스트 진행함 + void searchUser() { // setId() 메서드로 테스트 진행함 // Given Long userId1 = 1L; Long userId2 = 2L; @@ -159,9 +159,9 @@ void searchUser() { // setId() 메서드 임시 삽입하여 테스트 진행함 String name = "John"; // 사용자 목록 생성 - TestUser user1 = TestUser.createUser("John", "profile1.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); - TestUser user2 = TestUser.createUser("John Doe", "profile2.jpg", "keycode456", 170, 65, 1, 35, BaseNutrition.createNutrition(2000, 300, 80, 80)); - TestUser user3 = TestUser.createUser("John Doo", "profile3.jpg", "keycode789", 160, 55, 1, 25, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user1 = User.createUser("John", "profile1.jpg", "keycode123", 175, 70, 0, 30, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user2 = User.createUser("John Doe", "profile2.jpg", "keycode456", 170, 65, 1, 35, BaseNutrition.createNutrition(2000, 300, 80, 80)); + User user3 = User.createUser("John Doo", "profile3.jpg", "keycode789", 160, 55, 1, 25, BaseNutrition.createNutrition(2000, 300, 80, 80)); user1.setId(userId1); user2.setId(userId2); user3.setId(userId3);