Skip to content

Commit 1199bcb

Browse files
committed
test: #73 test 코드 작성
1 parent dbfa0f5 commit 1199bcb

File tree

3 files changed

+39
-56
lines changed

3 files changed

+39
-56
lines changed

src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java

-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import com.ajou.hertz.domain.user.dto.UserDto;
3838
import com.ajou.hertz.domain.user.dto.request.SignUpRequest;
3939
import com.ajou.hertz.domain.user.service.UserCommandService;
40-
import com.ajou.hertz.domain.user.service.UserProfileImageCommandService;
4140
import com.ajou.hertz.domain.user.service.UserQueryService;
4241
import com.ajou.hertz.util.ReflectionUtils;
4342
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -62,9 +61,6 @@ class UserControllerTest {
6261
@MockBean
6362
private UserQueryService userQueryService;
6463

65-
@MockBean
66-
private UserProfileImageCommandService userProfileImageCommandService;
67-
6864
private final MockMvc mvc;
6965

7066
private final ObjectMapper objectMapper;
@@ -275,7 +271,6 @@ public void securitySetUp() throws Exception {
275271
private void verifyEveryMocksShouldHaveNoMoreInteractions() {
276272
then(userCommandService).shouldHaveNoMoreInteractions();
277273
then(userQueryService).shouldHaveNoMoreInteractions();
278-
then(userProfileImageCommandService).shouldHaveNoMoreInteractions();
279274
}
280275

281276
private SignUpRequest createSignUpRequest(String email, String password, String phone) throws Exception {

src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java

+31-15
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import org.mockito.InjectMocks;
1818
import org.mockito.Mock;
1919
import org.mockito.junit.jupiter.MockitoExtension;
20+
import org.springframework.mock.web.MockMultipartFile;
2021
import org.springframework.security.crypto.password.PasswordEncoder;
2122
import org.springframework.test.context.event.annotation.BeforeTestMethod;
2223
import org.springframework.web.multipart.MultipartFile;
2324

24-
import com.ajou.hertz.common.file.dto.FileDto;
2525
import com.ajou.hertz.common.file.service.FileService;
2626
import com.ajou.hertz.common.kakao.dto.response.KakaoUserInfoResponse;
2727
import com.ajou.hertz.common.properties.HertzProperties;
@@ -183,22 +183,45 @@ static Stream<Arguments> testDataForCreateNewUserWithKakao() throws Exception {
183183
}
184184

185185
@Test
186-
void 프로필_이미지_변경_시_uploadProfileImage가_잘_호출되는지_확인한다() throws Exception {
186+
void 주어진_유저_ID와_이미지_URL로_유저의_프로필_이미지를_업데이트한다() throws Exception {
187187
// Given
188188
Long userId = 1L;
189-
MultipartFile profileImage = org.mockito.Mockito.mock(MultipartFile.class);
190-
User testUser = createUser(userId, "password", "kakaoUid");
191-
UserDto expectedUserDto = UserDto.from(testUser);
192-
given(userProfileImageCommandService.updateProfileImage(userId, profileImage)).willReturn(expectedUserDto);
189+
User user = createUser(userId, "password", "kakaoUid");
190+
String newProfileImageUrl = "https://new-profile-image-url";
191+
192+
MultipartFile profileImage = new MockMultipartFile("file", "test.jpg", "image/jpeg",
193+
"test image content".getBytes());
194+
195+
given(userQueryService.getById(userId)).willReturn(user);
196+
given(userProfileImageCommandService.updateProfileImage(user, profileImage)).willReturn(
197+
newProfileImageUrl);
193198

194199
// When
195200
UserDto result = sut.updateUserProfileImage(userId, profileImage);
196201

197202
// Then
198-
then(userProfileImageCommandService).should().updateProfileImage(userId, profileImage);
199-
assertThat(result).isEqualTo(expectedUserDto);
203+
then(userQueryService).should().getById(userId);
204+
then(userProfileImageCommandService).should().updateProfileImage(user, profileImage);
205+
assertThat(result.getProfileImageUrl()).isEqualTo(newProfileImageUrl);
206+
verifyEveryMocksShouldHaveNoMoreInteractions();
207+
}
208+
209+
@Test
210+
void 주어진_유저_ID와_이미지_URL로_유저의_프로필_이미지를_업데이트한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception {
211+
// Given
212+
Long userId = 1L;
213+
MultipartFile profileImage = new MockMultipartFile("file", "test.jpg", "image/jpeg",
214+
"test image content".getBytes());
200215

216+
given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class);
217+
218+
// When
219+
Throwable t = catchThrowable(() -> sut.updateUserProfileImage(userId, profileImage));
220+
221+
// Then
222+
then(userQueryService).should().getById(userId);
201223
verifyEveryMocksShouldHaveNoMoreInteractions();
224+
assertThat(t).isInstanceOf(UserNotFoundByIdException.class);
202225
}
203226

204227
@Test
@@ -308,11 +331,4 @@ private static KakaoUserInfoResponse createKakaoUserInfoResponse(String gender)
308331
private static KakaoUserInfoResponse createKakaoUserInfoResponse() {
309332
return createKakaoUserInfoResponse("male");
310333
}
311-
312-
private FileDto createFileDto() throws Exception {
313-
return ReflectionUtils.createFileDto(
314-
"test.jpg",
315-
"test-stored.jpg",
316-
"https://new-contactLink");
317-
}
318334
}

src/test/java/com/ajou/hertz/unit/domain/user/service/UserProfileImageCommandServiceTest.java

+8-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ajou.hertz.unit.domain.user.service;
22

3-
import static org.assertj.core.api.Assertions.*;
43
import static org.mockito.BDDMockito.*;
54

65
import java.time.LocalDate;
@@ -25,7 +24,6 @@
2524
import com.ajou.hertz.domain.user.constant.RoleType;
2625
import com.ajou.hertz.domain.user.entity.User;
2726
import com.ajou.hertz.domain.user.entity.UserProfileImage;
28-
import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException;
2927
import com.ajou.hertz.domain.user.repository.UserProfileImageRepository;
3028
import com.ajou.hertz.domain.user.repository.UserRepository;
3129
import com.ajou.hertz.domain.user.service.UserProfileImageCommandService;
@@ -66,7 +64,6 @@ public void setUp() {
6664
// given
6765
Long userId = 1L;
6866
User user = createUser(userId, "password", "kakaoUid");
69-
given(userQueryService.getById(userId)).willReturn(user);
7067

7168
MultipartFile newProfileImage = new MockMultipartFile(
7269
"profileImage",
@@ -80,10 +77,9 @@ public void setUp() {
8077
given(fileService.uploadFile(newProfileImage, uploadPath)).willReturn(uploadedFile);
8178

8279
// when
83-
sut.updateProfileImage(userId, newProfileImage);
80+
sut.updateProfileImage(user, newProfileImage);
8481

8582
// then
86-
then(userQueryService).should().getById(userId);
8783
then(fileService).should().uploadFile(newProfileImage, uploadPath);
8884
then(userProfileImageRepository).should().save(any(UserProfileImage.class));
8985
verifyEveryMocksShouldHaveNoMoreInteractions();
@@ -94,11 +90,10 @@ public void setUp() {
9490
// given
9591
Long userId = 1L;
9692
User user = createUser(userId, "password", "kakaoUid");
97-
given(userQueryService.getById(userId)).willReturn(user);
9893

9994
UserProfileImage oldProfileImage = createUserProfileImage(user, "oldOriginalName.jpg", "oldStoredName.jpg",
10095
"https://example.com/old-image-url");
101-
given(userProfileImageRepository.findById(userId)).willReturn(Optional.of(oldProfileImage));
96+
given(userProfileImageRepository.findByUser_Id(userId)).willReturn(Optional.of(oldProfileImage));
10297

10398
MultipartFile newProfileImage = new MockMultipartFile(
10499
"profileImage",
@@ -110,43 +105,20 @@ public void setUp() {
110105
String uploadPath = "user-profile-images/";
111106
FileDto uploadedFile = createFileDto();
112107

113-
given(fileService.uploadFile(eq(newProfileImage), eq(uploadPath))).willReturn(uploadedFile);
108+
given(fileService.uploadFile(newProfileImage, uploadPath)).willReturn(uploadedFile);
114109

115110
// when
116-
sut.updateProfileImage(userId, newProfileImage);
111+
sut.updateProfileImage(user, newProfileImage);
117112

118113
// then
119-
then(userQueryService).should().getById(userId);
120-
then(userProfileImageRepository).should().findById(userId);
121-
then(userProfileImageRepository).should().delete(any(UserProfileImage.class));
122-
then(fileService).should().deleteFile(anyString());
123-
then(fileService).should().uploadFile(eq(newProfileImage), eq(uploadPath));
114+
then(userProfileImageRepository).should().findByUser_Id(userId);
115+
then(userProfileImageRepository).should().delete(oldProfileImage);
116+
then(fileService).should().deleteFile(oldProfileImage.getStoredName());
117+
then(fileService).should().uploadFile(newProfileImage, uploadPath);
124118
then(userProfileImageRepository).should().save(any(UserProfileImage.class));
125119
verifyEveryMocksShouldHaveNoMoreInteractions();
126120
}
127121

128-
@Test
129-
void 프로필_이미지_변경_시_존재하지_않는_유저라면_예외를_반환한다() throws Exception {
130-
// given
131-
Long userId = 1L;
132-
given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class);
133-
134-
MultipartFile newProfileImage = new MockMultipartFile(
135-
"profileImage",
136-
"newProfile.jpg",
137-
"image/jpeg",
138-
"new image content".getBytes()
139-
);
140-
141-
// when
142-
Throwable t = catchThrowable(() -> sut.updateProfileImage(userId, newProfileImage));
143-
144-
// then
145-
assertThat(t).isInstanceOf(UserNotFoundByIdException.class);
146-
then(userQueryService).should().getById(userId);
147-
verifyEveryMocksShouldHaveNoMoreInteractions();
148-
}
149-
150122
private void verifyEveryMocksShouldHaveNoMoreInteractions() {
151123
then(userQueryService).shouldHaveNoMoreInteractions();
152124
then(userRepository).shouldHaveNoMoreInteractions();

0 commit comments

Comments
 (0)