-
Notifications
You must be signed in to change notification settings - Fork 1
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
✅ Refactor: UserServiceTest Mock으로 리팩토링 (#26) #27
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96318df
:bug: Fix: User image 속성 누락 수정 (#26)
win-luck 0773ebf
:white_check_mark: Refactor: UserServiceTest Mock으로 리팩토링 (검색 제외) (#26)
win-luck edb2303
:bug: Fix: UserController import 정리 및 UserService 검색기능 버그 수정 (#26)"
win-luck 2e8afd8
:recycle: Refactor: UserServiceTest 검색기능 Mock 적용 및 User에 SetId() 추가 (…
win-luck 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
211 changes: 118 additions & 93 deletions
211
src/test/java/com/diareat/diareat/service/UserServiceTest.java
This file contains 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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
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.
stubbing을 사용하셨군요! 참고해서 FoodServiceTest도 mocking으로 리팩토링 하겠습니다!