Skip to content
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 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/diareat/diareat/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public class User {
private List<FavoriteFood> 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;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}

// 회원 탈퇴
Expand Down Expand Up @@ -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));
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/com/diareat/diareat/service/FoodServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FoodService 테스트 쪽도 수정해주셔서 감사합니다~


//when
Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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);

Expand All @@ -177,15 +177,15 @@ 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));

}

@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)));
Expand All @@ -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)));
Expand Down