-
Notifications
You must be signed in to change notification settings - Fork 1
[test/#37] 로그인 테스트 #43
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
Open
kjeongh
wants to merge
19
commits into
develop
Choose a base branch
from
test/#37
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4125898
test: repository단위테스트 작성
kjeongh 96e9d46
style: google-java-format
b879f6d
test: 로그인 service 테스트
kjeongh 65734dd
style: google-java-format
059144f
fix: 불필요한 코드 삭제
kjeongh 3299d07
fix: merge하며 변경된 오류사항 수정
kjeongh aed3be4
style: google-java-format
83d025f
test: 로그인 성공 service 테스트
kjeongh 04aa8ec
test: login 주석처리 후 임시 커밋
kjeongh 6484ec6
fix: @Autowired로 Mockmvc 주입
kjeongh ba5a76b
test: Builder로 mockMvc초기화, Security관련 삭제
kjeongh c90b430
fix: 로그인 로직 주석처리 해제
kjeongh 0ae2403
test: user repository 테스트코드 작성
kjeongh 7c677c9
style: google-java-format
2a44d0e
fix: 단위테스트간 영향 제거
kjeongh 4aba5e8
fix: given/then문 수정
kjeongh a2863cd
Merge remote-tracking branch 'origin/test/#37' into test/#37
kjeongh da59dcb
fix: 메소드명 알맞게 수정 및 불필요 import문 삭제
kjeongh 926551c
style: google-java-format
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/techeer/cokkiri/domain/user/exception/UserPasswordWrongException.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,10 @@ | ||
| package com.techeer.cokkiri.domain.user.exception; | ||
|
|
||
| import com.techeer.cokkiri.global.error.ErrorCode; | ||
| import com.techeer.cokkiri.global.error.exception.BusinessException; | ||
|
|
||
| public class UserPasswordWrongException extends BusinessException { | ||
| public UserPasswordWrongException() { | ||
| super(ErrorCode.USER_PASSWORD_NOT_MATCH); | ||
| } | ||
| } |
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
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
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 |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |
| import javax.persistence.EntityListeners; | ||
| import javax.persistence.MappedSuperclass; | ||
| import lombok.*; | ||
| import org.springframework.data.annotation.CreatedDate; | ||
| import org.springframework.data.annotation.LastModifiedDate; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
| import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
|
||
| @Getter | ||
|
|
@@ -14,9 +14,9 @@ | |
| @EntityListeners(AuditingEntityListener.class) | ||
| @ToString | ||
| public class BaseEntity { | ||
| @CreatedDate private LocalDateTime createdAt; | ||
| @CreationTimestamp private LocalDateTime createdAt; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 CreationTimestamp, UpdateTimestamp 같은거는 위에 AuditingEntityListener 가 필요할까요? 만약 필요없다면 지우고 설정파일도 필요가 없어질거같네요 |
||
|
|
||
| @LastModifiedDate private LocalDateTime updatedAt; | ||
| @UpdateTimestamp private LocalDateTime updatedAt; | ||
|
|
||
| private boolean isDeleted; | ||
|
|
||
|
|
||
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
76 changes: 76 additions & 0 deletions
76
src/test/java/com/techeer/cokkiri/domain/user/Controller/UserControllerTest.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,76 @@ | ||
| package com.techeer.cokkiri.domain.user.Controller; | ||
|
|
||
| import static com.techeer.cokkiri.fixture.UserFixtures.USER_LOGIN_REQUEST; | ||
| import static com.techeer.cokkiri.global.result.ResultCode.USER_LOGIN_SUCCESS; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.client.match.MockRestRequestMatchers.content; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import com.techeer.cokkiri.domain.user.controller.UserController; | ||
| import com.techeer.cokkiri.domain.user.entity.User; | ||
| import com.techeer.cokkiri.domain.user.service.LoginService; | ||
| import com.techeer.cokkiri.domain.user.service.UserService; | ||
| import com.techeer.cokkiri.global.result.ResultResponse; | ||
| import com.techeer.cokkiri.global.util.JsonUtil; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
| import org.springframework.web.context.WebApplicationContext; | ||
| import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
|
||
| @AutoConfigureMockMvc | ||
| @WebMvcTest(UserController.class) | ||
| public class UserControllerTest { | ||
|
|
||
| private final JsonUtil jsonUtil = new JsonUtil(); | ||
|
|
||
| @MockBean UserService userService; | ||
|
|
||
| @MockBean LoginService loginService; | ||
|
|
||
| @Mock User mockUser; | ||
|
|
||
| private MockMvc mockMvc; | ||
|
|
||
| @BeforeEach | ||
| void setUp(WebApplicationContext applicationContext) { | ||
| mockMvc = | ||
| MockMvcBuilders.webAppContextSetup(applicationContext) | ||
| .addFilter(new CharacterEncodingFilter(StandardCharsets.UTF_8.name(), true)) | ||
| .build(); | ||
| } | ||
|
|
||
| // 미완성 테스트 - login메소드 오류 | ||
| @Test | ||
| @DisplayName("로그인에 성공할 경우 ResultResponse(") | ||
| void loginTest() throws Exception { | ||
| // given - 회원가입 | ||
| when(loginService.isValidPassword(any())).thenReturn(true); | ||
| when(userService.findByUsername(any())).thenReturn(mockUser); | ||
| when(mockUser.getId()).thenReturn(1L); | ||
|
|
||
| mockMvc | ||
| .perform( | ||
| post("/api/v1/users/login") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(jsonUtil.toJsonString(USER_LOGIN_REQUEST))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect( | ||
| MockMvcResultMatchers.content() | ||
| .string(jsonUtil.toJsonString(ResultResponse.of(USER_LOGIN_SUCCESS)))) | ||
| .andDo(print()); // 전체 메시지 확인 | ||
| System.out.println(content()); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/test/java/com/techeer/cokkiri/domain/user/repository/UserRepositoryTest.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,43 @@ | ||
| package com.techeer.cokkiri.domain.user.repository; | ||
|
|
||
| import static com.techeer.cokkiri.fixture.UserFixtures.DEFAULT_USER; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.techeer.cokkiri.domain.user.entity.User; | ||
| 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.autoconfigure.orm.jpa.DataJpaTest; | ||
|
|
||
| @DataJpaTest | ||
| public class UserRepositoryTest { | ||
|
|
||
| @Autowired private UserRepository userRepository; | ||
|
|
||
| private User user; | ||
|
|
||
| @BeforeEach | ||
| void saveUser() {} | ||
|
|
||
| @Test | ||
| @DisplayName("user의 존재여부를 username으로 확인한다.") | ||
| void existsByUsername() { | ||
| user = DEFAULT_USER; | ||
| userRepository.save(user); | ||
| assertTrue(userRepository.existsByUsername(user.getUsername())); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("username으로 user를 찾는다.") | ||
| void findByUsername() { | ||
| // given | ||
| user = DEFAULT_USER; | ||
| user = userRepository.save(user); | ||
| User savedUser = userRepository.findByUsername(user.getUsername()).orElseThrow(); | ||
|
|
||
| // then | ||
| assertEquals(user, savedUser); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/test/java/com/techeer/cokkiri/domain/user/service/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,82 @@ | ||
| package com.techeer.cokkiri.domain.user.service; | ||
|
|
||
| import static com.techeer.cokkiri.fixture.UserFixtures.DEFAULT_USER; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.techeer.cokkiri.domain.user.dto.UserDto; | ||
| import com.techeer.cokkiri.domain.user.entity.User; | ||
| import com.techeer.cokkiri.domain.user.exception.UserPasswordWrongException; | ||
| import com.techeer.cokkiri.domain.user.mapper.UserMapper; | ||
| import com.techeer.cokkiri.domain.user.repository.UserRepository; | ||
| import com.techeer.cokkiri.global.util.PasswordUtil; | ||
| 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; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class UserServiceTest { | ||
|
|
||
| @Mock private UserRepository userRepository; | ||
|
|
||
| @Mock private UserMapper userMapper; | ||
|
|
||
| @Mock private PasswordUtil passwordUtil; | ||
|
|
||
| @Mock private UserService userService; | ||
|
|
||
| @InjectMocks private LoginService loginService; | ||
|
|
||
| private User user; | ||
| private UserDto.LoginRequest loginRequest; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| user = DEFAULT_USER; | ||
|
|
||
| loginRequest = | ||
| UserDto.LoginRequest.builder() | ||
| .username(user.getUsername()) | ||
| .password(user.getPassword()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Nested | ||
| class loginTest { | ||
| @Test | ||
| @DisplayName("username은 존재하나 비밀번호가 다를 경우 로그인 실패한다.") | ||
| void wrongPassword() { | ||
| // given | ||
| when(userService.findByUsername(any())).thenReturn(user); | ||
| when(passwordUtil.isSamePassword(any(), any())).thenReturn(false); | ||
|
|
||
| // then | ||
| assertThrows( | ||
| UserPasswordWrongException.class, | ||
| () -> { | ||
| loginService.isValidPassword(loginRequest); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("올바른 username과 비밀번호 입력시 로그인 성공한다.") | ||
| void loginSuccess() { | ||
| // given | ||
| when(userService.findByUsername(any())).thenReturn(user); | ||
| when(passwordUtil.isSamePassword(any(), any())).thenReturn(true); | ||
|
|
||
| // when | ||
| boolean isValid = loginService.isValidPassword(loginRequest); | ||
|
|
||
| // then | ||
| assertTrue(isValid); | ||
| } | ||
| } | ||
| } |
23 changes: 22 additions & 1 deletion
23
src/test/java/com/techeer/cokkiri/fixture/UserFixtures.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 |
|---|---|---|
| @@ -1,14 +1,35 @@ | ||
| package com.techeer.cokkiri.fixture; | ||
|
|
||
| import com.techeer.cokkiri.domain.user.dto.UserDto; | ||
| import com.techeer.cokkiri.domain.user.entity.User; | ||
|
|
||
| public class UserFixtures { | ||
| public static final User STUDY_MANAGER_FIXTURE = | ||
| User.builder() | ||
| .username("javaStudyManager") | ||
| .bio("자바를 좋아합니다.") | ||
| .imageUrl("url") | ||
| .nickname("javaMania") | ||
| .password("qwer123!") | ||
| .build(); | ||
|
|
||
| public static final User DEFAULT_USER = | ||
| User.builder() | ||
| .username("DefaultUser1") | ||
| .bio("테스트용 디폴트 유저") | ||
| .nickname("default") | ||
| .password("Test1234!") | ||
| .build(); | ||
|
|
||
| public static final UserDto.LoginRequest USER_LOGIN_REQUEST = | ||
| UserDto.LoginRequest.builder() | ||
| .username(DEFAULT_USER.getUsername()) | ||
| .password(DEFAULT_USER.getPassword()) | ||
| .build(); | ||
|
|
||
| public static final UserDto.RegisterRequest USER_REGISTER_REQUEST = | ||
| UserDto.RegisterRequest.builder() | ||
| .username(DEFAULT_USER.getUsername()) | ||
| .password(DEFAULT_USER.getPassword()) | ||
| .nickname(DEFAULT_USER.getNickname()) | ||
| .build(); | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/test/java/com/techeer/cokkiri/global/util/JsonUtil.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,12 @@ | ||
| package com.techeer.cokkiri.global.util; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| public class JsonUtil { | ||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| public String toJsonString(Object object) throws JsonProcessingException { | ||
| return objectMapper.writeValueAsString(object); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -17,4 +17,4 @@ spring: | |
| generate-ddl: true | ||
| properties: | ||
| hibernate: | ||
| format_sql: true | ||
| format_sql: true | ||
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.
Uh oh!
There was an error while loading. Please reload this page.