Skip to content

Commit

Permalink
♻️ Refactor: UserServiceTest 검색기능 Mock 적용 및 User에 SetId() 추가 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 14, 2023
1 parent edb2303 commit 2e8afd8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/diareat/diareat/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 닉네임

Expand All @@ -44,8 +44,8 @@ public class TestUser {
private List<FavoriteFood> 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;
Expand All @@ -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;
}
}
30 changes: 15 additions & 15 deletions src/test/java/com/diareat/diareat/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +23,7 @@
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class TestUserServiceTest {
class UserServiceTest {

@InjectMocks
private UserService userService;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -151,17 +151,17 @@ void deleteUser() {
}

@Test
void searchUser() { // setId() 메서드 임시 삽입하여 테스트 진행함
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));
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);
Expand Down

0 comments on commit 2e8afd8

Please sign in to comment.