From 579c2f0170e36eefe2047a23a8a2bd0078ed55df Mon Sep 17 00:00:00 2001 From: Wonjeong Kim Date: Sat, 23 Mar 2024 18:14:03 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20PhotoBoardControllerTest=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PhotoBoardControllerTest.java | 126 ++++++++++-------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/src/test/java/server/inuappcenter/kr/controller/PhotoBoardControllerTest.java b/src/test/java/server/inuappcenter/kr/controller/PhotoBoardControllerTest.java index 1780dc6..b6e827d 100644 --- a/src/test/java/server/inuappcenter/kr/controller/PhotoBoardControllerTest.java +++ b/src/test/java/server/inuappcenter/kr/controller/PhotoBoardControllerTest.java @@ -3,31 +3,38 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.core.io.ClassPathResource; import org.springframework.mock.web.MockMultipartFile; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import server.inuappcenter.kr.common.data.dto.CommonResponseDto; import server.inuappcenter.kr.controller.boardController.PhotoBoardController; import server.inuappcenter.kr.data.dto.request.PhotoBoardRequestDto; +import server.inuappcenter.kr.data.dto.response.BoardResponseDto; import server.inuappcenter.kr.data.dto.response.PhotoBoardResponseDto; +import server.inuappcenter.kr.service.boardService.AdditionalBoardService; import server.inuappcenter.kr.service.boardService.BoardService; -import server.inuappcenter.kr.service.boardService.impl.PhotoBoardServiceImpl; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.verify; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -39,7 +46,8 @@ public class PhotoBoardControllerTest { @MockBean private BoardService boardService; @MockBean - private PhotoBoardServiceImpl photoBoardServiceImpl; + @Qualifier(value = "PhotoBoardServiceImpl") + private AdditionalBoardService additionalBoardService; public Map makeMockImageMap() { @@ -59,6 +67,8 @@ public Map makeMockImageMap() { .images(makeMockImageMap()) .build(); + private final CommonResponseDto expectedCommonRes = new CommonResponseDto("id has been successfully modified."); + // @WithMockUser // @DisplayName("PhotoBoard 가져오기 테스트") // @Test @@ -117,60 +127,62 @@ public void deleteBoardTest() throws Exception { verify(boardService).deleteBoard(givenId); } -// @WithMockUser -// @DisplayName("PhotoBoard 목록 가져오기 테스트") -// @Test -// public void findAllBoardTest() throws Exception { -// // given -// List introBoardResponseDtoList = new ArrayList<>(); -// for (int i = 0; i < 10; i++) { -// introBoardResponseDtoList.add(expectedDto); -// } -// given(photoBoardService.findAllPhotoBoard()).willReturn(introBoardResponseDtoList); -// // when -// mockMvc.perform(get("/photo-board/public/all-boards-contents")) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$..board_id").exists()) -// .andExpect(jsonPath("$..body").exists()) -// .andExpect(jsonPath("$..images").exists()) -// .andDo(print()); -// // then -// verify(photoBoardService).findAllPhotoBoard(); -// } + @WithMockUser + @DisplayName("PhotoBoard 목록 가져오기 테스트") + @Test + public void findAllBoardTest() throws Exception { + // given + List introBoardResponseDtoList = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + introBoardResponseDtoList.add(expectedDto); + } + given(additionalBoardService.findBoardList(null)).willReturn(introBoardResponseDtoList); + // when + mockMvc.perform(get("/photo-board/public/all-boards-contents")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$..id").exists()) + .andExpect(jsonPath("$..body").exists()) + .andExpect(jsonPath("$..images").exists()) + .andDo(print()); + // then + verify(additionalBoardService).findBoardList(null); + } -// @WithMockUser -// @DisplayName("PhotoBoard 수정 테스트") -// @Test -// public void updateBoardTest() throws Exception { -// // given -// given(photoBoardService.updatePhotoBoard(any(PhotoBoardRequestDto.class), eq(givenId))).willReturn(expectedDto); -// String imagePath = "test/image.jpg"; -// ClassPathResource resource = new ClassPathResource(imagePath); -// -// MockMultipartFile file = new MockMultipartFile("multipartFiles", "filename1.jpg", "text/plain", resource.getInputStream().readAllBytes()); -// -// MultiValueMap formData = new LinkedMultiValueMap<>(); -// formData.add("body", givenDto.getBody()); -// -// MockMultipartHttpServletRequestBuilder builder = -// MockMvcRequestBuilders.multipart("/photo-board?id=1"); -// builder.with(request -> { -// request.setMethod("PATCH"); -// return request; -// }); -// -// // when -// mockMvc.perform(builder -// .file(file) -// .params(formData) -// .with(csrf())) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$.board_id").exists()) -// .andExpect(jsonPath("$.body").exists()) -// .andExpect(jsonPath("$.images").exists()) -// .andDo(print()); -// -// // then -// verify(photoBoardService).updatePhotoBoard(any(PhotoBoardRequestDto.class), eq(givenId)); -// } + @WithMockUser + @DisplayName("PhotoBoard 수정 테스트") + @Test + public void updateBoardTest() throws Exception { + // given + List givenImageId = new ArrayList<>(); + givenImageId.add(1L); + givenImageId.add(2L); + + given(boardService.updateBoard(eq(givenId), eq(givenImageId), any(PhotoBoardRequestDto.class))).willReturn(expectedCommonRes); + String imagePath = "test/image.jpg"; + ClassPathResource resource = new ClassPathResource(imagePath); + + MockMultipartFile file = new MockMultipartFile("multipartFiles", "filename1.jpg", "text/plain", resource.getInputStream().readAllBytes()); + + MultiValueMap formData = new LinkedMultiValueMap<>(); + formData.add("body", givenDto.getBody()); + + MockMultipartHttpServletRequestBuilder builder = + MockMvcRequestBuilders.multipart("/photo-board/1,2?board_id=1"); + builder.with(request -> { + request.setMethod("PATCH"); + return request; + }); + + // when + mockMvc.perform(builder + .file(file) + .params(formData) + .with(csrf())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.msg").exists()) + .andDo(print()); + + // then + verify(boardService).updateBoard(eq(givenId), eq(givenImageId), any(PhotoBoardRequestDto.class)); + } }