
테스트코드를 작성하면서, form-data타입으로 DTO 객체와 Multipartfile 객체를 받는 컨트롤러 계층에서 마주한 이슈와 해결과정을 기록하고자 한다.
사용자의 프로필을 수정하는 컨트롤러에서, 수정할 닉네임은 DTO 객체로, 프로필 사진은 Multipartfile로 받아서 서비스계층의 메서드를 호출한다.
UserController

DTO 객체를 objectMapper를 통해 DTO -> string -> multipartfile 객체로 전환하여, 테스트코드를 작성하는 방법을 탐색할 수 있었다.UserObjectFixture

MockMultipartFile file = new MockMultipartFile("file", fileName, contentType, "test data".getBytes(StandardCharsets.UTF_8));
MockMultipartFile requestDto = new MockMultipartFile("requestDto", "request.json", "application/json", mapper.writeValueAsBytes(updateRequest));
mockMvc.perform(multipart(HttpMethod.PUT,"/api/v1/user/profile")
.file(file)
.file(requestDto)
.with(csrf()))
.andExpect(status().isOk())
.andDo(print());

테스트가 정상적으로 수행되었다.
전체 코드
