Skip to content

Commit

Permalink
[Fix] IntroBoardControllerTest 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
NARUBROWN committed Mar 22, 2024
1 parent ce4024e commit ac03c8b
Showing 1 changed file with 94 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
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.IntroBoardController;
import server.inuappcenter.kr.data.dto.request.IntroBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.dto.response.IntroBoardResponseDto;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.BoardService;
import server.inuappcenter.kr.service.boardService.impl.IntroBoardServiceImpl;

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;
Expand All @@ -38,7 +45,8 @@ public class IntroBoardControllerTest {
@MockBean
private BoardService boardService;
@MockBean
private IntroBoardServiceImpl introBoardServiceImpl;
@Qualifier(value = "IntroBoardServiceImpl")
private AdditionalBoardService additionalBoardService;


public Map<Long, String> makeMockImageMap() {
Expand All @@ -61,25 +69,27 @@ public Map<Long, String> makeMockImageMap() {
.images(makeMockImageMap())
.build();

// @WithMockUser
// @DisplayName("IntroBoard 가져오기 테스트")
// @Test
// public void getBoardTest() throws Exception {
// // given
// given(introBoardService.getIntroBoard(givenId)).willReturn(expectedDto);
// // when
// mockMvc.perform(get("/introduction-board/public/" + givenId))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.id").exists())
// .andExpect(jsonPath("$.subTitle").exists())
// .andExpect(jsonPath("$.title").exists())
// .andExpect(jsonPath("$.appleStoreLink").exists())
// .andExpect(jsonPath("$.androidStoreLink").exists())
// .andExpect(jsonPath("$.images").exists())
// .andDo(print());
// // then
// verify(introBoardService).getIntroBoard(givenId);
// }
private final CommonResponseDto expectedCommonRes = new CommonResponseDto("id has been successfully modified.");

@WithMockUser
@DisplayName("IntroBoard 가져오기 테스트")
@Test
public void getBoardTest() throws Exception {
// given
given(boardService.findBoard(givenId)).willReturn(expectedDto);
// when
mockMvc.perform(get("/introduction-board/public/" + givenId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").exists())
.andExpect(jsonPath("$.subTitle").exists())
.andExpect(jsonPath("$.title").exists())
.andExpect(jsonPath("$.appleStoreLink").exists())
.andExpect(jsonPath("$.androidStoreLink").exists())
.andExpect(jsonPath("$.images").exists())
.andDo(print());
// then
verify(boardService).findBoard(givenId);
}

@WithMockUser
@DisplayName("IntroBoard 저장 테스트")
Expand Down Expand Up @@ -126,70 +136,67 @@ public void deleteBoardTest() throws Exception {
verify(boardService).deleteBoard(givenId);
}

// @WithMockUser
// @DisplayName("IntroBoard 목록 가져오기 테스트")
// @Test
// public void findAllBoardTest() throws Exception {
// // given
// List<IntroBoardResponseDto> introBoardResponseDtoList = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// introBoardResponseDtoList.add(expectedDto);
// }
// given(introBoardService.findAllIntroBoard()).willReturn(introBoardResponseDtoList);
// // when
// mockMvc.perform(get("/introduction-board/public/all-boards-contents"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$..id").exists())
// .andExpect(jsonPath("$..subTitle").exists())
// .andExpect(jsonPath("$..title").exists())
// .andExpect(jsonPath("$..appleStoreLink").exists())
// .andExpect(jsonPath("$..androidStoreLink").exists())
// .andExpect(jsonPath("$..images").exists())
// .andDo(print());
// // then
// verify(introBoardService).findAllIntroBoard();
// }

// @WithMockUser
// @DisplayName("IntroBoard 수정 테스트")
// @Test
// public void updateBoardTest() throws Exception {
// // given
// given(introBoardService.updateIntroBoard(any(IntroBoardRequestDto.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<String, String> formData = new LinkedMultiValueMap<>();
// formData.add("title", givenDto.getTitle());
// formData.add("subTitle", givenDto.getSubTitle());
// formData.add("appleStoreLink", givenDto.getAppleStoreLink());
// formData.add("androidStoreLink", givenDto.getAndroidStoreLink());
// formData.add("body", givenDto.getBody());
//
// MockMultipartHttpServletRequestBuilder builder =
// MockMvcRequestBuilders.multipart("/introduction-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("$.id").exists())
// .andExpect(jsonPath("$.subTitle").exists())
// .andExpect(jsonPath("$.title").exists())
// .andExpect(jsonPath("$.appleStoreLink").exists())
// .andExpect(jsonPath("$.androidStoreLink").exists())
// .andExpect(jsonPath("$.images").exists())
// .andDo(print());
//
// // then
// verify(introBoardService).updateIntroBoard(any(IntroBoardRequestDto.class), eq(givenId));
// }
@WithMockUser
@DisplayName("IntroBoard 목록 가져오기 테스트")
@Test
public void findAllBoardTest() throws Exception {
// given
List<BoardResponseDto> introBoardResponseDtoList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
introBoardResponseDtoList.add(expectedDto);
}
given(additionalBoardService.findBoardList(null)).willReturn(introBoardResponseDtoList);
// when
mockMvc.perform(get("/introduction-board/public/all-boards-contents"))
.andExpect(status().isOk())
.andExpect(jsonPath("$..id").exists())
.andExpect(jsonPath("$..subTitle").exists())
.andExpect(jsonPath("$..title").exists())
.andExpect(jsonPath("$..appleStoreLink").exists())
.andExpect(jsonPath("$..androidStoreLink").exists())
.andExpect(jsonPath("$..images").exists())
.andDo(print());
// then
verify(additionalBoardService).findBoardList(null);
}

@WithMockUser
@DisplayName("IntroBoard 수정 테스트")
@Test
public void updateBoardTest() throws Exception {
List<Long> givenImageId = new ArrayList<>();
givenImageId.add(1L);
givenImageId.add(2L);
// given
given(boardService.updateBoard(eq(givenId), eq(givenImageId), any(IntroBoardRequestDto.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<String, String> formData = new LinkedMultiValueMap<>();
formData.add("title", givenDto.getTitle());
formData.add("subTitle", givenDto.getSubTitle());
formData.add("appleStoreLink", givenDto.getAppleStoreLink());
formData.add("androidStoreLink", givenDto.getAndroidStoreLink());
formData.add("body", givenDto.getBody());

MockMultipartHttpServletRequestBuilder builder =
MockMvcRequestBuilders.multipart("/introduction-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())
.andDo(print());

// then
verify(boardService).updateBoard(eq(givenId), eq(givenImageId), any(IntroBoardRequestDto.class));
}
}

0 comments on commit ac03c8b

Please sign in to comment.