Skip to content
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

[test] 유저 서비스 레이어 테스트 #24

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.snsIntegrationFeedService.user.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Optional;

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.security.crypto.password.PasswordEncoder;

import com.snsIntegrationFeedService.certificateCode.entity.CertificateCode;
import com.snsIntegrationFeedService.certificateCode.repository.CertificateCodeRepository;
import com.snsIntegrationFeedService.common.error.CustomErrorCode;
import com.snsIntegrationFeedService.common.exception.CustomException;
import com.snsIntegrationFeedService.user.dto.SignupRequestDto;
import com.snsIntegrationFeedService.user.entity.User;
import com.snsIntegrationFeedService.user.repository.UserRepository;

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

@InjectMocks
private UserService userService;

@Mock
private UserRepository userRepository;

@Mock
private CertificateCodeRepository certificateCodeRepository;

@Mock
private PasswordEncoder passwordEncoder;

@Mock
private PasswordValidation passwordValidation;

@Test
void 회원가입_test() throws Exception {
// given
SignupRequestDto signupRequestDto = SignupRequestDto.builder()
.account("test")
.email("test@test.com")
.password("test123")
.build();

// stub 1
when(userRepository.findByAccount(any())).thenReturn(Optional.empty());

// stub 2
when(passwordEncoder.encode(any())).thenReturn("encodedPassword");

// when
userService.signup(signupRequestDto);

// then
verify(userRepository).save(any(User.class));
verify(certificateCodeRepository).save(any(CertificateCode.class));
}

@Test
void 회원가입_실패_중복_test() throws Exception {
// given
SignupRequestDto signupRequestDto = SignupRequestDto.builder()
.account("test")
.email("test@test.com")
.password("test123")
.build();

User user = User.builder()
.account("test")
.email("test@test.com")
.password("test123")
.build();

// stub 1
when(userRepository.findByAccount(any())).thenReturn(Optional.of(user));

// when
CustomException exception = assertThrows(CustomException.class, () -> {
userService.signup(signupRequestDto);
});

// then
assertEquals(CustomErrorCode.USER_ALREADY_EXIST, exception.getErrorCode());
}
}
Loading