Skip to content

Commit 71ada03

Browse files
committed
[BE] refactor(#610): mock 객체 네이밍 변경
1 parent c64d1f9 commit 71ada03

12 files changed

+219
-218
lines changed

backend/src/test/java/com/example/backend/auth/api/controller/auth/AuthControllerTest.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AuthControllerTest extends MockTestConfig {
5151
private MockMvc mockMvc;
5252

5353
@Autowired
54-
private AuthService authService;
54+
private AuthService mockAuthService;
5555

5656
@Autowired
5757
private UserRepository userRepository;
@@ -63,7 +63,7 @@ class AuthControllerTest extends MockTestConfig {
6363
private ObjectMapper objectMapper;
6464

6565
@Autowired
66-
private RankingService rankingService;
66+
private RankingService mockRankingService;
6767

6868
@AfterEach
6969
void tearDown() {
@@ -127,7 +127,7 @@ void registerSuccessTest() throws Exception {
127127
.fcmToken("token")
128128
.build();
129129

130-
when(authService.register(any(AuthServiceRegisterRequest.class), any(User.class)))
130+
when(mockAuthService.register(any(AuthServiceRegisterRequest.class), any(User.class)))
131131
.thenReturn(AuthLoginResponse.builder()
132132
.accessToken(extendedAtk)
133133
.refreshToken(extendedRtk)
@@ -152,7 +152,7 @@ void validUserTokenRequestWithDrawThenUserDelete() throws Exception {
152152
Map<String, String> map = TokenUtil.createTokenMap(savedUser);
153153
String accessToken = jwtService.generateAccessToken(map, savedUser);
154154

155-
when(authService.register(any(AuthServiceRegisterRequest.class), any(User.class))).thenReturn(AuthLoginResponse.builder()
155+
when(mockAuthService.register(any(AuthServiceRegisterRequest.class), any(User.class))).thenReturn(AuthLoginResponse.builder()
156156
.accessToken(accessToken)
157157
//.refreshToken(refreshToken)
158158
.build()
@@ -162,7 +162,7 @@ void validUserTokenRequestWithDrawThenUserDelete() throws Exception {
162162
.message("reason")
163163
.build();
164164

165-
doNothing().when(authService).userDelete(any(User.class), any(String.class));
165+
doNothing().when(mockAuthService).userDelete(any(User.class), any(String.class));
166166

167167
// when
168168
mockMvc.perform(post("/auth/delete")
@@ -182,7 +182,7 @@ void userInfoSuccessTest() throws Exception {
182182
UserInfoAndRankingResponse savedUser = UserInfoAndRankingResponse.of(userRepository.save(user), 1L);
183183

184184

185-
when(authService.getUserByInfo(user.getPlatformId(), GITHUB)).thenReturn(savedUser);
185+
when(mockAuthService.getUserByInfo(user.getPlatformId(), GITHUB)).thenReturn(savedUser);
186186

187187
HashMap<String, String> map = new HashMap<>();
188188
map.put("role", user.getRole().name());
@@ -249,8 +249,8 @@ void userInfoWhenInvalidAuthority() throws Exception {
249249
String accessToken = jwtService.generateAccessToken(map, savedUser);
250250

251251
// when
252-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.of(savedUser));
253-
when(authService.updateUserPage(any(Long.class))).thenReturn(UserUpdatePageResponse.builder()
252+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.of(savedUser));
253+
when(mockAuthService.updateUserPage(any(Long.class))).thenReturn(UserUpdatePageResponse.builder()
254254
.name(savedUser.getName())
255255
.profileImageUrl(savedUser.getProfileImageUrl())
256256
.build());
@@ -275,7 +275,7 @@ void userInfoWhenInvalidAuthority() throws Exception {
275275
String accessToken = jwtService.generateAccessToken(map, savedUser);
276276

277277
// when
278-
when(authService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY));
278+
when(mockAuthService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY));
279279

280280
// then
281281
mockMvc.perform(get("/auth/update")
@@ -303,8 +303,8 @@ void userInfoWhenInvalidAuthority() throws Exception {
303303
.blogLink("https://test.tistory.com/").build())
304304
.build();
305305
// when
306-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
307-
doNothing().when(authService).updateUser(any(UserUpdateServiceRequest.class));
306+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
307+
doNothing().when(mockAuthService).updateUser(any(UserUpdateServiceRequest.class));
308308

309309
// then
310310
mockMvc.perform(post("/auth/update")
@@ -335,8 +335,8 @@ void userInfoWhenInvalidAuthority() throws Exception {
335335
.build())
336336
.build();
337337
// when
338-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
339-
doNothing().when(authService).updateUser(any(UserUpdateServiceRequest.class));
338+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
339+
doNothing().when(mockAuthService).updateUser(any(UserUpdateServiceRequest.class));
340340

341341
// then
342342
mockMvc.perform(post("/auth/update")
@@ -365,9 +365,9 @@ void userInfoWhenInvalidAuthority() throws Exception {
365365
.build();
366366

367367
// when
368-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
368+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
369369
doThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY))
370-
.when(authService)
370+
.when(mockAuthService)
371371
.updateUser(any(UserUpdateServiceRequest.class));
372372

373373
// then
@@ -391,8 +391,8 @@ void userInfoWhenInvalidAuthority() throws Exception {
391391
String accessToken = jwtService.generateAccessToken(map, savedUser);
392392

393393
// when
394-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
395-
doNothing().when(authService).updatePushAlarmYn(any(Long.class), any(boolean.class));
394+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
395+
doNothing().when(mockAuthService).updatePushAlarmYn(any(Long.class), any(boolean.class));
396396

397397
// then
398398
mockMvc.perform(get("/auth/update/pushAlarmYn" + "/" + true)
@@ -412,7 +412,7 @@ void userInfoWhenInvalidAuthority() throws Exception {
412412
String accessToken = jwtService.generateAccessToken(map, savedUser);
413413

414414
// when
415-
when(authService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY));
415+
when(mockAuthService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY));
416416
// then
417417
mockMvc.perform(get("/auth/update/pushAlarmYn" + "/" + true)
418418
.contentType(MediaType.APPLICATION_JSON)
@@ -428,7 +428,7 @@ void userInfoWhenInvalidAuthority() throws Exception {
428428
// given
429429
UserNameRequest request = UserFixture.generateUserNameRequest("이정우");
430430

431-
doNothing().when(authService).nickNameDuplicationCheck(any(UserNameRequest.class));
431+
doNothing().when(mockAuthService).nickNameDuplicationCheck(any(UserNameRequest.class));
432432

433433
// when & then
434434
mockMvc.perform(post("/auth/check-nickname")
@@ -447,7 +447,7 @@ void userInfoWhenInvalidAuthority() throws Exception {
447447

448448
UserNameRequest request = UserFixture.generateUserNameRequest(inValidName);
449449

450-
doNothing().when(authService).nickNameDuplicationCheck(any(UserNameRequest.class));
450+
doNothing().when(mockAuthService).nickNameDuplicationCheck(any(UserNameRequest.class));
451451

452452
// when
453453
mockMvc.perform(post("/auth/check-nickname")
@@ -466,7 +466,7 @@ void userInfoWhenInvalidAuthority() throws Exception {
466466

467467
UserNameRequest request = UserFixture.generateUserNameRequest(inValidName);
468468

469-
doNothing().when(authService).nickNameDuplicationCheck(any(UserNameRequest.class));
469+
doNothing().when(mockAuthService).nickNameDuplicationCheck(any(UserNameRequest.class));
470470

471471
// when
472472
mockMvc.perform(post("/auth/check-nickname")

backend/src/test/java/com/example/backend/study/api/controller/bookmark/StudyBookmarkControllerTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class StudyBookmarkControllerTest extends MockTestConfig {
3636
private MockMvc mockMvc;
3737

3838
@Autowired
39-
private StudyBookmarkService studyBookmarkService;
39+
private StudyBookmarkService mockStudyBookmarkService;
4040

4141
@Autowired
42-
private AuthService authService;
42+
private AuthService mockAuthService;
4343

4444
@Autowired
4545
private JwtService jwtService;
@@ -52,8 +52,8 @@ class StudyBookmarkControllerTest extends MockTestConfig {
5252
Map<String, String> map = TokenUtil.createTokenMap(user);
5353
String accessToken = jwtService.generateAccessToken(map, user);
5454

55-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
56-
when(studyBookmarkService.selectUserBookmarkList(any(Long.class), any(Long.class), any(Long.class)))
55+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
56+
when(mockStudyBookmarkService.selectUserBookmarkList(any(Long.class), any(Long.class), any(Long.class)))
5757
.thenReturn(List.of(BookmarkInfoResponse.builder().build()));
5858

5959
// when
@@ -77,8 +77,8 @@ class StudyBookmarkControllerTest extends MockTestConfig {
7777
Map<String, String> map = TokenUtil.createTokenMap(user);
7878
String accessToken = jwtService.generateAccessToken(map, user);
7979

80-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.of(user));
81-
when(studyBookmarkService.selectUserBookmarkList(any(Long.class), any(Long.class), any(Long.class)))
80+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.of(user));
81+
when(mockStudyBookmarkService.selectUserBookmarkList(any(Long.class), any(Long.class), any(Long.class)))
8282
.thenReturn(new ArrayList<>());
8383

8484
// when
@@ -102,7 +102,7 @@ class StudyBookmarkControllerTest extends MockTestConfig {
102102
Map<String, String> map = TokenUtil.createTokenMap(user);
103103
String accessToken = jwtService.generateAccessToken(map, user);
104104

105-
when(authService.findUserInfo(any(User.class)))
105+
when(mockAuthService.findUserInfo(any(User.class)))
106106
.thenThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY));
107107

108108
// when
@@ -147,8 +147,8 @@ class StudyBookmarkControllerTest extends MockTestConfig {
147147
String accessToken = jwtService.generateAccessToken(map, user);
148148

149149
// when
150-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
151-
doNothing().when(studyBookmarkService).handleBookmark(any(Long.class), any(Long.class));
150+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
151+
doNothing().when(mockStudyBookmarkService).handleBookmark(any(Long.class), any(Long.class));
152152

153153
// when
154154
mockMvc.perform(get("/bookmarks/study/" + studyInfoId)
@@ -169,7 +169,7 @@ class StudyBookmarkControllerTest extends MockTestConfig {
169169
String accessToken = jwtService.generateAccessToken(map, user);
170170

171171
// when
172-
when(authService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.AUTH_NOT_FOUND));
172+
when(mockAuthService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.AUTH_NOT_FOUND));
173173

174174
// when
175175
mockMvc.perform(get("/bookmarks/study/" + studyInfoId)
@@ -191,8 +191,8 @@ class StudyBookmarkControllerTest extends MockTestConfig {
191191
String accessToken = jwtService.generateAccessToken(map, user);
192192

193193
// when
194-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
195-
when(studyBookmarkService.getIsMyBookMark(any(Long.class), any(Long.class)))
194+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
195+
when(mockStudyBookmarkService.getIsMyBookMark(any(Long.class), any(Long.class)))
196196
.thenReturn(StudyBookmarkFixture.createIsMyBookmarkResponse(user.getId(), studyInfoId));
197197

198198
// when
@@ -214,7 +214,7 @@ class StudyBookmarkControllerTest extends MockTestConfig {
214214
String accessToken = jwtService.generateAccessToken(map, user);
215215

216216
// when
217-
when(authService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.AUTH_NOT_FOUND));
217+
when(mockAuthService.findUserInfo(any(User.class))).thenThrow(new AuthException(ExceptionMessage.AUTH_NOT_FOUND));
218218

219219
// when
220220
mockMvc.perform(get("/bookmarks/study/" + studyInfoId + "/my-bookmark")

backend/src/test/java/com/example/backend/study/api/controller/category/info/CategoryControllerTest.java

+23-22
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ class CategoryControllerTest extends MockTestConfig {
5454
private UserRepository userRepository;
5555

5656
@Autowired
57-
private CategoryService categoryService;
57+
private CategoryService mockCategoryService;
58+
5859
@Autowired
59-
private AuthService authService;
60+
private AuthService mockAuthService;
6061

6162
@Autowired
6263
private StudyCategoryRepository studyCategoryRepository;
@@ -84,8 +85,8 @@ void tearDown() {
8485
String accessToken = jwtService.generateAccessToken(map, user);
8586

8687
// when
87-
when(authService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
88-
doNothing().when(categoryService).registerCategory(request);
88+
when(mockAuthService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
89+
doNothing().when(mockCategoryService).registerCategory(request);
8990

9091
// then
9192
mockMvc.perform(post("/category")
@@ -111,8 +112,8 @@ void tearDown() {
111112
.build();
112113

113114
// when
114-
when(authService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
115-
doNothing().when(categoryService).registerCategory(request);
115+
when(mockAuthService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
116+
doNothing().when(mockCategoryService).registerCategory(request);
116117

117118
//when, then
118119
mockMvc.perform(post("/category")
@@ -140,8 +141,8 @@ void tearDown() {
140141
.build();
141142

142143
// when
143-
when(authService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
144-
doNothing().when(categoryService).registerCategory(request);
144+
when(mockAuthService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
145+
doNothing().when(mockCategoryService).registerCategory(request);
145146

146147
//when, then
147148
mockMvc.perform(post("/category")
@@ -169,8 +170,8 @@ void tearDown() {
169170
.build();
170171

171172
//when
172-
when(authService.authenticate(any(Long.class), any(User.class))).thenReturn(UserInfoResponse.of(savedUser));
173-
doNothing().when(categoryService).updateCategory(any(CategoryUpdateRequest.class), any(Long.class));
173+
when(mockAuthService.authenticate(any(Long.class), any(User.class))).thenReturn(UserInfoResponse.of(savedUser));
174+
doNothing().when(mockCategoryService).updateCategory(any(CategoryUpdateRequest.class), any(Long.class));
174175

175176
//then
176177
mockMvc.perform(patch("/category/" + studyCategory.getId())
@@ -199,8 +200,8 @@ void tearDown() {
199200
.build();
200201

201202
// when
202-
when(authService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
203-
doNothing().when(categoryService).updateCategory(request, studyCategory.getId());
203+
when(mockAuthService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
204+
doNothing().when(mockCategoryService).updateCategory(request, studyCategory.getId());
204205

205206
//when, then
206207
mockMvc.perform(post("/category")
@@ -231,8 +232,8 @@ void tearDown() {
231232
.build();
232233

233234
// when
234-
when(authService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
235-
doNothing().when(categoryService).updateCategory(request, studyCategory.getId());
235+
when(mockAuthService.findUserInfo(any())).thenReturn(UserInfoResponse.of(user));
236+
doNothing().when(mockCategoryService).updateCategory(request, studyCategory.getId());
236237

237238
//when, then
238239
mockMvc.perform(patch("/category/" + studyCategory.getId())
@@ -261,7 +262,7 @@ void tearDown() {
261262

262263
//when
263264
doThrow(new CategoryException(ExceptionMessage.CATEGORY_NOT_FOUND))
264-
.when(categoryService)
265+
.when(mockCategoryService)
265266
.updateCategory(any(), any());
266267

267268
//then
@@ -286,8 +287,8 @@ void tearDown() {
286287
= studyCategoryRepository.save(StudyCategoryFixture.createDefaultPublicStudyCategory("name"));
287288

288289
//when
289-
when(authService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
290-
doNothing().when(categoryService).deleteCategory(any(Long.class));
290+
when(mockAuthService.findUserInfo(any(User.class))).thenReturn(UserInfoResponse.builder().build());
291+
doNothing().when(mockCategoryService).deleteCategory(any(Long.class));
291292

292293
//then
293294
mockMvc.perform(delete("/category/" + studyCategory.getId())
@@ -311,7 +312,7 @@ void tearDown() {
311312

312313
//when
313314
doThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY))
314-
.when(authService)
315+
.when(mockAuthService)
315316
.findUserInfo(any(User.class));
316317

317318
//then
@@ -335,8 +336,8 @@ void tearDown() {
335336
CategoryListAndCursorIdxResponse response
336337
= StudyCategoryFixture.generateCategoryListAndCursorIdxResponse(3);
337338

338-
when(authService.authenticate(any(Long.class), any(User.class))).thenReturn(UserInfoResponse.builder().build());
339-
when(categoryService.selectCategoryList(any(Long.class), any(Long.class), any(Long.class)))
339+
when(mockAuthService.authenticate(any(Long.class), any(User.class))).thenReturn(UserInfoResponse.builder().build());
340+
when(mockCategoryService.selectCategoryList(any(Long.class), any(Long.class), any(Long.class)))
340341
.thenReturn(response);
341342

342343
// when
@@ -364,7 +365,7 @@ void tearDown() {
364365

365366
//when
366367
doThrow(new AuthException(ExceptionMessage.UNAUTHORIZED_AUTHORITY))
367-
.when(authService)
368+
.when(mockAuthService)
368369
.findUserInfo(any(User.class));
369370

370371
mockMvc.perform(get("/category/" + studyInfo.getId())
@@ -412,7 +413,7 @@ void tearDown() {
412413
Map<String, String> map = TokenUtil.createTokenMap(user);
413414
String accessToken = jwtService.generateAccessToken(map, user);
414415

415-
when(categoryService.selectCategoryList()).thenReturn(List.of(CategoryResponse.builder().id(categoryId).name(categoryName).build()));
416+
when(mockCategoryService.selectCategoryList()).thenReturn(List.of(CategoryResponse.builder().id(categoryId).name(categoryName).build()));
416417

417418
// when
418419
mockMvc.perform(get("/category/")

0 commit comments

Comments
 (0)