From 1a4e22905008a8dfa6a8bf2571d946345c96b752 Mon Sep 17 00:00:00 2001 From: ohyujeong Date: Tue, 25 Jun 2024 15:42:14 +0900 Subject: [PATCH] =?UTF-8?q?[Test]=20UserService=20=EB=8B=A8=EC=9C=84=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flab/tess/service/UserServiceTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/test/java/com/flab/tess/service/UserServiceTest.java diff --git a/src/test/java/com/flab/tess/service/UserServiceTest.java b/src/test/java/com/flab/tess/service/UserServiceTest.java new file mode 100644 index 0000000..f39dd13 --- /dev/null +++ b/src/test/java/com/flab/tess/service/UserServiceTest.java @@ -0,0 +1,91 @@ +package com.flab.tess.service; + +import com.flab.tess.domain.User; +import com.flab.tess.dto.LoginRequest; +import com.flab.tess.repository.UserRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class UserServiceTest { + + @Mock + private UserRepository userRepository; + + @InjectMocks + private UserService userService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void 회원_가입() { + User user = mock(User.class); + User encodedUser = mock(User.class); + when(user.encodePassword()).thenReturn(encodedUser); + when(userRepository.save(encodedUser)).thenReturn(encodedUser); + + User result = userService.join(user); + + assertNotNull(result); + verify(userRepository, times(1)).save(encodedUser); + } + + @Test + void 로그인() { + String loginId = "testLoginId"; + String password = "testPassword"; + + LoginRequest loginRequest = new LoginRequest(loginId, password); + User user = mock(User.class); + + when(userRepository.findByLoginId(loginId)).thenReturn(Optional.of(user)); + when(user.checkPassword(password)).thenReturn(true); + + User loginResult = userService.login(loginRequest); + + assertNotNull(loginResult); + } + + @Test + void 잘못된_패스워드_로그인_예외() { + + String loginId = "testLoginId"; + String password = "invalidPassword"; + LoginRequest loginRequest = new LoginRequest(loginId, password); + User user = mock(User.class); + + when(userRepository.findByLoginId(loginId)).thenReturn(Optional.of(user)); + when(user.checkPassword(password)).thenReturn(false); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + userService.login(loginRequest); + }); + + assertEquals("잘못된 비밀번호입니다.", exception.getMessage()); + } + + @Test + void 가입되지_않은_아이디_예외() { + String loginId = "nonExistentUser"; + String password = "password"; + LoginRequest loginRequest = new LoginRequest(loginId, password); + + when(userRepository.findByLoginId(loginId)).thenReturn(Optional.empty()); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + userService.login(loginRequest); + }); + + assertEquals("가입되지 않은 아이디 입니다.", exception.getMessage()); + } +}