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);