#24 simple sns: 회원가입, 로그인 테스트코드 수정

This commit is contained in:
haerong22
2022-11-01 00:43:18 +09:00
parent d165c8d824
commit edf0ebc0d3
2 changed files with 16 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static com.example.sns.exception.ErrorCode.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -53,7 +54,7 @@ public class UserControllerTest {
String username = "username";
String password = "password";
when(userService.join(username, password)).thenThrow(new SnsApplicationException(ErrorCode.DUPLICATED_USER_NAME, ""));
when(userService.join(username, password)).thenThrow(new SnsApplicationException(DUPLICATED_USER_NAME));
mockMvc.perform(post("/api/v1/users/join")
.contentType(MediaType.APPLICATION_JSON)
@@ -81,7 +82,7 @@ public class UserControllerTest {
String username = "username";
String password = "password";
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
when(userService.login(username, password)).thenThrow(new SnsApplicationException(USER_NOT_FOUND));
mockMvc.perform(post("/api/v1/users/login")
.contentType(MediaType.APPLICATION_JSON)
@@ -95,7 +96,7 @@ public class UserControllerTest {
String username = "username";
String password = "password";
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
when(userService.login(username, password)).thenThrow(new SnsApplicationException(INVALID_PASSWORD));
mockMvc.perform(post("/api/v1/users/login")
.contentType(MediaType.APPLICATION_JSON)

View File

@@ -12,9 +12,9 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.Optional;
import static com.example.sns.exception.ErrorCode.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@SpringBootTest
@@ -36,7 +36,7 @@ class UserServiceTest {
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(UserEntityFixture.get(username, password));
assertDoesNotThrow(() -> userService.join(username, password));
}
@@ -52,10 +52,12 @@ class UserServiceTest {
when(encoder.encode(password)).thenReturn("encrypt_password");
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
assertThrows(
SnsApplicationException e = assertThrows(
SnsApplicationException.class,
() -> userService.join(username, password)
);
assertEquals(DUPLICATED_USER_NAME, e.getErrorCode());
}
@Test
@@ -66,6 +68,7 @@ class UserServiceTest {
UserEntity fixture = UserEntityFixture.get(username, password);
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
when(encoder.matches(password, fixture.getPassword())).thenReturn(true);
assertDoesNotThrow(() -> userService.login(username, password));
}
@@ -77,10 +80,12 @@ class UserServiceTest {
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
assertThrows(
SnsApplicationException e = assertThrows(
SnsApplicationException.class,
() -> userService.login(username, password)
);
assertEquals(USER_NOT_FOUND, e.getErrorCode());
}
@Test
@@ -93,9 +98,11 @@ class UserServiceTest {
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
assertThrows(
SnsApplicationException e = assertThrows(
SnsApplicationException.class,
() -> userService.login(username, wrongPassword)
);
assertEquals(INVALID_PASSWORD, e.getErrorCode());
}
}