Skip to content

Commit 12d52c1

Browse files
committed
test(controller-test): ControllerTestSupport 리팩토링에 따른 테스트
- 컨트롤러 테스트시 print() 일괄 적용 - print()시 한글 깨짐 현상 해결
1 parent f8fda90 commit 12d52c1

14 files changed

+278
-310
lines changed

src/test/java/com/somemore/domains/center/controller/CenterProfileCommandApiControllerTest.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
package com.somemore.domains.center.controller;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyString;
5+
import static org.mockito.BDDMockito.given;
6+
import static org.mockito.BDDMockito.willDoNothing;
7+
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
412
import com.somemore.domains.center.dto.request.CenterProfileUpdateRequestDto;
513
import com.somemore.domains.center.usecase.command.UpdateCenterProfileUseCase;
614
import com.somemore.global.imageupload.usecase.ImageUploadUseCase;
715
import com.somemore.support.ControllerTestSupport;
816
import com.somemore.support.annotation.WithMockCustomUser;
17+
import java.util.UUID;
918
import org.junit.jupiter.api.DisplayName;
1019
import org.junit.jupiter.api.Test;
11-
import org.springframework.beans.factory.annotation.Autowired;
1220
import org.springframework.boot.test.mock.mockito.MockBean;
1321
import org.springframework.http.MediaType;
1422
import org.springframework.mock.web.MockHttpServletRequest;
1523
import org.springframework.mock.web.MockMultipartFile;
16-
import org.springframework.test.web.servlet.MockMvc;
1724
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
1825
import org.springframework.test.web.servlet.request.RequestPostProcessor;
1926

20-
import java.util.UUID;
21-
22-
import static org.mockito.ArgumentMatchers.any;
23-
import static org.mockito.ArgumentMatchers.anyString;
24-
import static org.mockito.BDDMockito.given;
25-
import static org.mockito.BDDMockito.willDoNothing;
26-
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
27-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
28-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
29-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31-
3227
class CenterProfileCommandApiControllerTest extends ControllerTestSupport {
3328

34-
@Autowired
35-
private MockMvc mockMvc;
36-
37-
@Autowired
38-
private ObjectMapper objectMapper;
39-
4029
@MockBean
4130
private UpdateCenterProfileUseCase updateCenterProfileUseCase;
4231

@@ -92,7 +81,6 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
9281
.header("Authorization", "Bearer access-token"))
9382

9483
//then
95-
.andDo(print())
9684
.andExpect(status().isOk())
9785
.andExpect(jsonPath("$.code").value(200))
9886
.andExpect(jsonPath("$.data").isEmpty())

src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.somemore.domains.center.controller;
22

3+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER;
4+
import static org.mockito.Mockito.times;
5+
import static org.mockito.Mockito.verify;
6+
import static org.mockito.Mockito.when;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
311
import com.somemore.domains.center.dto.response.CenterProfileResponseDto;
412
import com.somemore.domains.center.usecase.query.CenterQueryUseCase;
513
import com.somemore.global.exception.BadRequestException;
614
import com.somemore.support.ControllerTestSupport;
15+
import java.util.List;
16+
import java.util.UUID;
717
import org.junit.jupiter.api.BeforeEach;
818
import org.junit.jupiter.api.DisplayName;
919
import org.junit.jupiter.api.Test;
1020
import org.springframework.boot.test.mock.mockito.MockBean;
1121
import org.springframework.http.MediaType;
1222

13-
import java.util.List;
14-
import java.util.UUID;
15-
16-
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER;
17-
import static org.mockito.Mockito.*;
18-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
20-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
21-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22-
2323
class CenterQueryApiControllerTest extends ControllerTestSupport {
2424

2525
@MockBean
@@ -53,16 +53,18 @@ void getCenterProfile() throws Exception {
5353
get("/api/center/profile/{centerId}", centerId)
5454
.contentType(MediaType.APPLICATION_JSON)
5555
)
56-
.andDo(print())
5756
.andExpect(status().isOk())
5857
.andExpect(jsonPath("$.code").value("200"))
5958
.andExpect(jsonPath("$.message").value("기관 프로필 조회 성공"))
6059
.andExpect(jsonPath("$.data.center_id").value(centerId.toString())) // center_id로 수정
6160
.andExpect(jsonPath("$.data.name").value("Test Center"))
62-
.andExpect(jsonPath("$.data.contact_number").value("010-1234-5678")) // contact_number로 수정
63-
.andExpect(jsonPath("$.data.img_url").value("http://example.com/image.jpg")) // img_url로 수정
61+
.andExpect(jsonPath("$.data.contact_number").value(
62+
"010-1234-5678")) // contact_number로 수정
63+
.andExpect(jsonPath("$.data.img_url").value(
64+
"http://example.com/image.jpg")) // img_url로 수정
6465
.andExpect(jsonPath("$.data.introduce").value("This is a test center."))
65-
.andExpect(jsonPath("$.data.homepage_link").value("http://example.com")) // homepage_link로 수정
66+
.andExpect(jsonPath("$.data.homepage_link").value(
67+
"http://example.com")) // homepage_link로 수정
6668
.andExpect(jsonPath("$.data.prefer_items").isArray()); // prefer_items로 수정
6769

6870
verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(centerId);
@@ -81,7 +83,6 @@ void getCenterProfile_NotFound() throws Exception {
8183
get("/api/center/profile/{centerId}", nonExistentCenterId)
8284
.contentType(MediaType.APPLICATION_JSON)
8385
)
84-
.andDo(print())
8586
.andExpect(status().isBadRequest())
8687
.andExpect(jsonPath("$.status").value("400"))
8788
.andExpect(jsonPath("$.detail").value("존재하지 않는 기관입니다."));

src/test/java/com/somemore/domains/center/controller/PreferItemCommandApiControllerTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.somemore.domains.center.controller;
22

3+
import static org.mockito.BDDMockito.given;
4+
import static org.mockito.Mockito.doThrow;
5+
import static org.mockito.Mockito.verify;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
311
import com.somemore.domains.center.dto.request.PreferItemCreateRequestDto;
412
import com.somemore.domains.center.dto.response.PreferItemCreateResponseDto;
513
import com.somemore.domains.center.usecase.command.CreatePreferItemUseCase;
614
import com.somemore.domains.center.usecase.command.DeletePreferItemUseCase;
715
import com.somemore.global.exception.BadRequestException;
816
import com.somemore.support.ControllerTestSupport;
917
import com.somemore.support.annotation.WithMockCustomUser;
18+
import java.util.UUID;
1019
import org.junit.jupiter.api.DisplayName;
1120
import org.junit.jupiter.api.Test;
1221
import org.springframework.boot.test.mock.mockito.MockBean;
1322
import org.springframework.http.MediaType;
1423

15-
import java.util.UUID;
16-
17-
import static org.mockito.BDDMockito.given;
18-
import static org.mockito.Mockito.doThrow;
19-
import static org.mockito.Mockito.verify;
20-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
21-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
23-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24-
2524
class PreferItemCommandApiControllerTest extends ControllerTestSupport {
2625

2726
@MockBean

src/test/java/com/somemore/domains/community/controller/CommunityBoardCommandApiControllerTest.java

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package com.somemore.domains.community.controller;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyString;
5+
import static org.mockito.BDDMockito.given;
6+
import static org.mockito.BDDMockito.willDoNothing;
7+
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
12+
413
import com.somemore.domains.community.dto.request.CommunityBoardCreateRequestDto;
514
import com.somemore.domains.community.dto.request.CommunityBoardUpdateRequestDto;
615
import com.somemore.domains.community.usecase.board.CreateCommunityBoardUseCase;
@@ -9,38 +18,18 @@
918
import com.somemore.global.imageupload.usecase.ImageUploadUseCase;
1019
import com.somemore.support.ControllerTestSupport;
1120
import com.somemore.support.annotation.WithMockCustomUser;
21+
import java.util.UUID;
1222
import org.junit.jupiter.api.DisplayName;
1323
import org.junit.jupiter.api.Test;
14-
import org.springframework.beans.factory.annotation.Autowired;
1524
import org.springframework.boot.test.mock.mockito.MockBean;
1625
import org.springframework.http.MediaType;
1726
import org.springframework.mock.web.MockHttpServletRequest;
1827
import org.springframework.mock.web.MockMultipartFile;
19-
import org.springframework.test.web.servlet.MockMvc;
2028
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
2129
import org.springframework.test.web.servlet.request.RequestPostProcessor;
2230

23-
import java.util.UUID;
24-
25-
import static org.mockito.ArgumentMatchers.any;
26-
import static org.mockito.ArgumentMatchers.anyString;
27-
import static org.mockito.BDDMockito.given;
28-
import static org.mockito.BDDMockito.willDoNothing;
29-
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
30-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
31-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
32-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
33-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
34-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35-
3631
public class CommunityBoardCommandApiControllerTest extends ControllerTestSupport {
3732

38-
@Autowired
39-
private MockMvc mockMvc;
40-
41-
@Autowired
42-
private ObjectMapper objectMapper;
43-
4433
@MockBean
4534
private CreateCommunityBoardUseCase createCommunityBoardUseCase;
4635

@@ -86,10 +75,10 @@ void createCommunityBoard_success() throws Exception {
8675

8776
//when
8877
mockMvc.perform(multipart("/api/community-board")
89-
.file(requestData)
90-
.file(imageFile)
91-
.contentType(MULTIPART_FORM_DATA)
92-
.header("Authorization", "Bearer access-token"))
78+
.file(requestData)
79+
.file(imageFile)
80+
.contentType(MULTIPART_FORM_DATA)
81+
.header("Authorization", "Bearer access-token"))
9382
//then
9483
.andExpect(status().isOk())
9584
.andExpect(jsonPath("$.code").value(201))
@@ -138,13 +127,12 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
138127

139128
//when
140129
mockMvc.perform(builder
141-
.file(requestData)
142-
.file(imageFile)
143-
.contentType(MULTIPART_FORM_DATA)
144-
.header("Authorization", "Bearer access-token"))
130+
.file(requestData)
131+
.file(imageFile)
132+
.contentType(MULTIPART_FORM_DATA)
133+
.header("Authorization", "Bearer access-token"))
145134

146135
//then
147-
.andDo(print())
148136
.andExpect(status().isOk())
149137
.andExpect(jsonPath("$.code").value(200))
150138
.andExpect(jsonPath("$.data").isEmpty())
@@ -157,13 +145,13 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
157145
void deleteCommunityBoard_success() throws Exception {
158146
//given
159147
Long communityBoardId = 1L;
160-
willDoNothing().given(deleteCommunityBoardUseCase).deleteCommunityBoard(any(UUID.class), any());
148+
willDoNothing().given(deleteCommunityBoardUseCase)
149+
.deleteCommunityBoard(any(UUID.class), any());
161150

162151
//when
163152
mockMvc.perform(delete("/api/community-board/{id}", communityBoardId)
164-
.header("Authorization", "Bearer access-token"))
153+
.header("Authorization", "Bearer access-token"))
165154
//then
166-
.andDo(print())
167155
.andExpect(status().isOk())
168156
.andExpect(jsonPath("$.code").value(200))
169157
.andExpect(jsonPath("$.message").value("커뮤니티 게시글 삭제 성공"))

src/test/java/com/somemore/domains/community/controller/CommunityBoardQueryApiControllerTest.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
package com.somemore.domains.community.controller;
22

3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyInt;
5+
import static org.mockito.BDDMockito.given;
6+
import static org.mockito.Mockito.times;
7+
import static org.mockito.Mockito.verify;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
312
import com.somemore.domains.community.dto.response.CommunityBoardDetailResponseDto;
413
import com.somemore.domains.community.dto.response.CommunityBoardResponseDto;
514
import com.somemore.domains.community.usecase.board.CommunityBoardQueryUseCase;
615
import com.somemore.support.ControllerTestSupport;
16+
import java.util.Collections;
17+
import java.util.UUID;
718
import org.junit.jupiter.api.DisplayName;
819
import org.junit.jupiter.api.Test;
9-
import org.springframework.beans.factory.annotation.Autowired;
1020
import org.springframework.boot.test.mock.mockito.MockBean;
1121
import org.springframework.data.domain.Page;
1222
import org.springframework.data.domain.PageImpl;
1323
import org.springframework.http.MediaType;
14-
import org.springframework.test.web.servlet.MockMvc;
15-
16-
import java.util.Collections;
17-
import java.util.UUID;
18-
19-
import static org.mockito.ArgumentMatchers.any;
20-
import static org.mockito.ArgumentMatchers.anyInt;
21-
import static org.mockito.BDDMockito.given;
22-
import static org.mockito.Mockito.times;
23-
import static org.mockito.Mockito.verify;
24-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
25-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
26-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2724

2825
public class CommunityBoardQueryApiControllerTest extends ControllerTestSupport {
2926

30-
@Autowired
31-
private MockMvc mockMvc;
32-
3327
@MockBean
3428
private CommunityBoardQueryUseCase communityBoardQueryUseCase;
3529

@@ -48,7 +42,7 @@ void getAll() throws Exception {
4842
//when
4943
//then
5044
mockMvc.perform(get("/api/community-boards")
51-
.accept(MediaType.APPLICATION_JSON))
45+
.accept(MediaType.APPLICATION_JSON))
5246
.andExpect(status().isOk())
5347
.andExpect(jsonPath("$.code").value(200))
5448
.andExpect(jsonPath("$.data").exists())
@@ -88,7 +82,8 @@ void getByWriterId() throws Exception {
8882
void getById() throws Exception {
8983
//given
9084
Long communityBoardId = 1L;
91-
CommunityBoardDetailResponseDto responseDto = CommunityBoardDetailResponseDto.builder().build();
85+
CommunityBoardDetailResponseDto responseDto = CommunityBoardDetailResponseDto.builder()
86+
.build();
9287
given(communityBoardQueryUseCase.getCommunityBoardDetail(any()))
9388
.willReturn(responseDto);
9489

0 commit comments

Comments
 (0)