Skip to content

Commit

Permalink
✅ Test: UserService 테스트코드 작성 및 테스트 완료 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 2, 2023
1 parent 49f543f commit 6525f4c
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/test/java/com/diareat/diareat/service/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.diareat.diareat.service;

import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.dto.CreateUserDto;
import com.diareat.diareat.user.dto.ResponseResearchUserDto;
import com.diareat.diareat.user.dto.ResponseUserDto;
import com.diareat.diareat.user.dto.UpdateUserDto;
import com.diareat.diareat.user.repository.UserRepository;
Expand All @@ -10,15 +11,15 @@
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 java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
@Transactional
class UserServiceTest {

@Autowired
Expand Down Expand Up @@ -50,13 +51,13 @@ void testSaveAndGetUserInfo() { // 회원정보 저장 및 조회
void testUpdateUserInfo() { // 회원정보 수정

// given
Long userId = 1L;
userService.saveUser(CreateUserDto.of("1", "testPassword", 180, 75, 1, 25));
Long id = userService.saveUser(CreateUserDto.of("1", "testPassword", 180, 75, 1, 25));
BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80);

// when
UpdateUserDto updateUserDto = UpdateUserDto.of(1L, "2", 185, 70, 75, null);
UpdateUserDto updateUserDto = UpdateUserDto.of(id, "2", 185, 70, 75, baseNutrition);
userService.updateUserInfo(updateUserDto);
ResponseUserDto responseUserDto = userService.getUserInfo(userId);
ResponseUserDto responseUserDto = userService.getUserInfo(id);

// then
assertNotNull(responseUserDto);
Expand All @@ -68,14 +69,13 @@ void testUpdateUserInfo() { // 회원정보 수정
@Test
void testDeleteUser() { // 회원 탈퇴
// given
Long userId = 1L;
userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));
Long id = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));

// when
userService.deleteUser(userId);
userService.deleteUser(id);

// then
assertEquals(Optional.empty(), userRepository.findById(userId));
assertNull(userRepository.findById(id).orElse(null));
}

@Test
Expand All @@ -86,7 +86,7 @@ void testSearchUserName() {

// when
String name = "testUser";
List<User> users = userService.searchUser(name);
List<ResponseResearchUserDto> users = userService.searchUser(name);

// then
assertEquals(1, users.size());
Expand All @@ -95,31 +95,27 @@ void testSearchUserName() {
@Test
void testFollowUser() { // 회원이 특정 회원 팔로우
// given
Long userId = 1L;
Long followId = 2L;
userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));
userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25));
Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));
Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25));

// when
userService.followUser(userId, followId);
userService.followUser(id1, id2);

// then
assertEquals(1, userRepository.findById(userId).get().getFollowings().size());
assertEquals(1, userRepository.findById(id1).get().getFollowings().size());
}

@Test
void testUnfollowUser() { // 회원이 특정 회원 팔로우 취소
// given
Long userId = 1L;
Long followId = 2L;
userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));
userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25));
Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25));
Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25));

// when
userService.followUser(userId, followId);
userService.unfollowUser(userId, followId);
userService.followUser(id1, id2);
userService.unfollowUser(id1, id2);

// then
assertEquals(0, userRepository.findById(userId).get().getFollowings().size());
assertEquals(0, userRepository.findById(id1).get().getFollowings().size());
}
}

0 comments on commit 6525f4c

Please sign in to comment.