-
Notifications
You must be signed in to change notification settings - Fork 1
[test] 사용자 기능 단위 테스트 작성 #38
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
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ce4b3a7
test: UserService 단위 테스트 작성(#35)
yereumi dafdcc2
fix: 메서드명 수정(#35)
yereumi 7246ae2
test: 리뷰 내용 반영(#35)
yereumi 31ef076
test: avatarUrl 수정(#35)
yereumi 82b29f4
test: 메서드명 수정(#35)
yereumi 018f951
test: 이너 클래스명 수정(#35)
yereumi 7521392
Merge branch 'dev' into test/35
yereumi 18dcf29
fix: 병합 충돌 해결(#35)
yereumi cb7d17d
Merge remote-tracking branch 'origin/test/35' into test/35
yereumi ec97015
fix: UserServiceTest Mock 추가(#35)
yereumi 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
This file contains hidden or 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
This file contains hidden or 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
131 changes: 131 additions & 0 deletions
131
src/test/java/com/specialwarriors/conal/UserServiceTest.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package com.specialwarriors.conal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import com.specialwarriors.conal.common.auth.oauth.GithubOAuth2WebClient; | ||
| import com.specialwarriors.conal.common.exception.GeneralException; | ||
| import com.specialwarriors.conal.user.domain.User; | ||
| import com.specialwarriors.conal.user.exception.UserException; | ||
| import com.specialwarriors.conal.user.repository.UserRepository; | ||
| import com.specialwarriors.conal.user.service.UserService; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.data.redis.core.ValueOperations; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class UserServiceTest { | ||
|
|
||
| @Mock | ||
| private UserRepository userRepository; | ||
|
|
||
| @Mock | ||
| private RedisTemplate<String, String> redisTemplate; | ||
|
|
||
| @Mock | ||
| private GithubOAuth2WebClient githubOAuth2WebClient; | ||
|
|
||
| @InjectMocks | ||
| private UserService userService; | ||
|
|
||
| private User mockUser; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| mockUser = new User(1, "홍길동", "fdsf"); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("유저 아이디로 사용자를 조회할 때") | ||
| class FindUserById { | ||
|
|
||
| @Test | ||
| @DisplayName("성공한다") | ||
| void success() { | ||
|
|
||
| // given | ||
| long userId = 1L; | ||
| given(userRepository.findById(userId)).willReturn(Optional.of(mockUser)); | ||
|
|
||
| // when | ||
| User result = userService.getUser(userId); | ||
|
|
||
| // then | ||
| assertThat(result).isEqualTo(mockUser); | ||
| verify(userRepository).findById(userId); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("시용자가 존재하지 않으면 예외를 던진다") | ||
| void throwsExceptionWhenUserNotFoundById() { | ||
|
|
||
| // given | ||
| long userId = 1L; | ||
| given(userRepository.findById(userId)).willReturn(Optional.empty()); | ||
|
|
||
| // when, then | ||
| assertThatThrownBy(() -> userService.getUser(userId)) | ||
| .isInstanceOf(GeneralException.class) | ||
| .extracting("exception") | ||
| .isEqualTo(UserException.USER_NOT_FOUND); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("사용자를 삭제할 때") | ||
| class DeleteUser { | ||
|
||
|
|
||
| @Test | ||
| @DisplayName("깃허브 토큰과 세션을 삭제한다") | ||
| void deletesGithubTokenAndSessionWhenUserIsDeleted() { | ||
|
|
||
| // given | ||
| long userId = 1L; | ||
| String githubTokenKey = "github:token:1"; | ||
| String githubTokenValue = "fake-access-token"; | ||
|
|
||
| given(userRepository.findById(userId)).willReturn(Optional.of(mockUser)); | ||
|
|
||
| ValueOperations<String, String> ops = mock(ValueOperations.class); | ||
| given(redisTemplate.opsForValue()).willReturn(ops); | ||
| given(ops.get(githubTokenKey)).willReturn(githubTokenValue); | ||
|
|
||
| // when | ||
| userService.deleteUser(userId); | ||
|
|
||
| // then | ||
| verify(githubOAuth2WebClient).unlink(githubTokenValue); | ||
| verify(redisTemplate).unlink(githubTokenKey); | ||
| verify(userRepository).deleteById(userId); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("사용자가 존재하지 않으면 예외를 던진다") | ||
| void throwsExceptionWhenDeletingNonexistentUser() { | ||
|
|
||
| // given | ||
| long userId = 1L; | ||
| given(userRepository.findById(userId)).willReturn(Optional.empty()); | ||
|
|
||
| // when, then | ||
| assertThatThrownBy(() -> userService.deleteUser(userId)) | ||
| .isInstanceOf(GeneralException.class) | ||
| .extracting("exception") | ||
| .isEqualTo(UserException.USER_NOT_FOUND); | ||
|
|
||
| verify(userRepository).findById(userId); | ||
|
||
| } | ||
| } | ||
|
|
||
| } | ||
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.
service의 메서드명을 repository에 맞추자는 예시는 위같은 형식을 의미했습니다.
getUser는 어떤 파라미터를 바탕으로 조회하는 지 의미를 표현함에 있어 메서드명이 모호하다고 생각합니다.