#24 simple sns: 예외 처리 및 테스트 코드 수정
This commit is contained in:
@@ -8,7 +8,8 @@ import org.springframework.http.HttpStatus;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum ErrorCode {
|
public enum ErrorCode {
|
||||||
|
|
||||||
DUPLICATED_USER_NAME(HttpStatus.CONFLICT, "Username is duplicated.")
|
DUPLICATED_USER_NAME(HttpStatus.CONFLICT, "Username is duplicated."),
|
||||||
|
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error.")
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.sns.exception;
|
|||||||
|
|
||||||
import com.example.sns.controller.response.Response;
|
import com.example.sns.controller.response.Response;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
@@ -16,4 +17,11 @@ public class GlobalControllerAdvice {
|
|||||||
return ResponseEntity.status(e.getErrorCode().getStatus())
|
return ResponseEntity.status(e.getErrorCode().getStatus())
|
||||||
.body(Response.error(e.getErrorCode().name()));
|
.body(Response.error(e.getErrorCode().name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(RuntimeException.class)
|
||||||
|
public ResponseEntity<?> applicationHandler(RuntimeException e) {
|
||||||
|
log.info("Error occurs {}", e.toString());
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.body(Response.error(ErrorCode.INTERNAL_SERVER_ERROR.name()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.example.sns.repository.UserEntityRepository;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -16,11 +17,12 @@ public class UserService {
|
|||||||
private final UserEntityRepository userEntityRepository;
|
private final UserEntityRepository userEntityRepository;
|
||||||
private final BCryptPasswordEncoder encoder;
|
private final BCryptPasswordEncoder encoder;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public User join(String username, String password){
|
public User join(String username, String password){
|
||||||
|
|
||||||
// username 확인
|
// username 확인
|
||||||
userEntityRepository.findByUsername(username).ifPresent(it -> {
|
userEntityRepository.findByUsername(username).ifPresent(it -> {
|
||||||
throw new SnsApplicationException(ErrorCode.DUPLICATED_USER_NAME, String.format("$s is duplicated", username));
|
throw new SnsApplicationException(ErrorCode.DUPLICATED_USER_NAME, String.format("%s is duplicated", username));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 회원가입 진행
|
// 회원가입 진행
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -25,12 +26,16 @@ class UserServiceTest {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private UserEntityRepository userEntityRepository;
|
private UserEntityRepository userEntityRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private BCryptPasswordEncoder encoder;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void 회원가입이_정상적으로_동작하는_경우() {
|
void 회원가입이_정상적으로_동작하는_경우() {
|
||||||
String username = "username";
|
String username = "username";
|
||||||
String password = "password";
|
String password = "password";
|
||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
||||||
|
when(encoder.encode(password)).thenReturn("encrypt_password");
|
||||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(UserEntityFixture.get(username, password)));
|
when(userEntityRepository.save(any())).thenReturn(Optional.of(UserEntityFixture.get(username, password)));
|
||||||
|
|
||||||
assertDoesNotThrow(() -> userService.join(username, password));
|
assertDoesNotThrow(() -> userService.join(username, password));
|
||||||
@@ -44,6 +49,7 @@ class UserServiceTest {
|
|||||||
UserEntity fixture = UserEntityFixture.get(username, password);
|
UserEntity fixture = UserEntityFixture.get(username, password);
|
||||||
|
|
||||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||||
|
when(encoder.encode(password)).thenReturn("encrypt_password");
|
||||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
|
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
|
|||||||
Reference in New Issue
Block a user