스프링 파일 업로드 테스트 방법
강준현
- java
- spring-boot
이 글의 목차
0. 들어가면서
파일 업로드 기능에 대한 단위 테스트를 작성해 본 경험이 없어서 관련된 내용을 정리하였다. 스프링 프레임워크를 사용해 개발하는 경우 MultipartFile 인터페이스를 통해 쉽게 파일 업로드 기능을 구현할 수 있다. MultipartFile 인터페이스에 대한 내용은 스프링 MultipartFile 인터페이스와 파일 업로드 포스트를 참고하길 바란다. DTO(Data Transfer Object) 클래스를 통해 파일 업로드하는 예제는 스프링 DTO(Data Transfer Object) 객체와 파일 업로드 포스트를 참고하길 바란다.
1. Test Target Method
세 가지 기능에 대한 테스트를 수행한다.
- 단일 이미지를 업로드
- 메서드 파라미터 변수 이름을
file로 지정한다.
- 메서드 파라미터 변수 이름을
- 다중 이미지를 업로드
- 메서드 파라미터 변수 이름을
files로 지정한다.
- 메서드 파라미터 변수 이름을
- DTO 클래스를 사용해 이미지와 다른 정보들을 함께 업로드
- 이름, 나이, 사진 정보를 프로퍼티로 지닌 클래스이다.
package action.in.blog.controller;
import action.in.blog.domain.Profile;
import action.in.blog.service.ProfileService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
public class ProfileController {
private final ProfileService profileService;
public ProfileController(ProfileService profileService) {
this.profileService = profileService;
}
@PostMapping("/profiles/image")
public void uploadProfileImage(MultipartFile file) {
profileService.uploadProfileImage(file);
}
@PostMapping("/profiles/images")
public void uploadProfileImages(List<MultipartFile> files) {
profileService.uploadProfileImages(files);
}
@PostMapping("/profiles")
public void uploadProfile(Profile profile) {
profileService.uploadProfile(profile);
}
}
다음 Profile 클래스를 살펴보겠다.
- 이름, 나이, 사진 프로퍼티를 가진다.
package action.in.blog.domain;
import org.springframework.web.multipart.MultipartFile;
public record Profile(
String name,
int age,
MultipartFile picture
) {
}
2. Tests
컨트롤러(controller)의 각 메서드들을 검증하기 위한 테스트 코드를 작성한다. 메서드들의 반환 타입이 void이므로 ProfileService 인스턴스를 테스트 더블(test double)로 사용하여 파일 업로드 여부를 검증한다.
2.1. MockMultipartFile Class
파일 업로드 테스트를 위해 MockMultipartFile 클래스를 사용한다. 테스트 객체 생성을 위해 생성자 함수를 살펴보겠다.
- name
- 스프링 API 엔드포인트(end point) 메서드에서 파일을 받을 때 사용하는 이름이다.
- 파라미터 이름과 동일하게 설정한다.
- @RequestParam 애너테이션을 사용한다면 파라미터 이름과 동일하게 설정한다.
- @ModelAttribute 애너테이션을 사용한다면 DTO 클래스 프로퍼티 이름과 동일하게 설정한다.
- originalFilename
- 실제 파일 이름을 의미한다.
- contentType
- 파일 타입을 의미한다.
- content
- 파일을 이진화(binary)하였을 때 바이트 배열(byte array) 값을 의미한다.
public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
Assert.hasLength(name, "Name must not be empty");
this.name = name;
this.originalFilename = (originalFilename != null ? originalFilename : "");
this.contentType = contentType;
this.content = (content != null ? content : new byte[0]);
}
2.2. Test uploadProfileImage Method
각 메서드를 테스트한다. uploadProfileImage 메서드를 먼저 테스트한다.
- 더미(dummy) MultipartFile 인스턴스를 생성한다.
- 파라미터 키는
file이다.
- 파라미터 키는
- profileService 스파이(spy) 객체에 전달된 파라미터 값을 확인한다.
- 파일 이름, 컨텐츠 타입, 이진 값을 확인한다.
package action.in.blog.controller;
import action.in.blog.domain.Profile;
import action.in.blog.service.ProfileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class ProfileControllerTest {
ProfileService profileService;
MockMvc sut;
@BeforeEach
void setUp() {
profileService = Mockito.mock(ProfileService.class);
sut = MockMvcBuilders
.standaloneSetup(new ProfileController(profileService))
.build();
}
@Test
public void uploadProfileImage() throws Exception {
var profileImage = new MockMultipartFile("file", "profile.jpg", "image/jpeg", "profile-image-binary".getBytes());
sut.perform(
multipart("/profiles/image")
.file(profileImage)
).andExpect(status().isOk());
var argumentCaptor = ArgumentCaptor.forClass(MultipartFile.class);
verify(profileService, times(1)).uploadProfileImage(argumentCaptor.capture());
var result = argumentCaptor.getValue();
assertEquals("profile.jpg", result.getOriginalFilename());
assertEquals("image/jpeg", result.getContentType());
assertArrayEquals("profile-image-binary".getBytes(), result.getBytes());
}
}
2.3. Test uploadProfileImages Method
uploadProfileImages 메서드를 테스트한다.
- 더미(dummy) MultipartFile 인스턴스를 생성한다.
- 파라미터 키는
files이다.
- 파라미터 키는
- profileService 스파이(spy) 객체에 전달된 파라미터 값을 확인한다.
- MultipartFile 리스트 정보를 확인한다.
- 리스트 사이즈, 각 파일 이름, 각 컨텐츠 타입, 각 이진 값을 확인한다.
package action.in.blog.controller;
import action.in.blog.domain.Profile;
import action.in.blog.service.ProfileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class ProfileControllerTest {
ProfileService profileService;
MockMvc sut;
@BeforeEach
void setUp() {
profileService = Mockito.mock(ProfileService.class);
sut = MockMvcBuilders
.standaloneSetup(new ProfileController(profileService))
.build();
}
@Test
public void uploadProfileImages() throws Exception {
var profileImage = new MockMultipartFile("files", "profile.jpg", "image/jpeg", "profile-image-binary".getBytes());
var backgroundImage = new MockMultipartFile("files", "background.jpg", "image/jpeg", "background-image-binary".getBytes());
sut.perform(
multipart("/profiles/images")
.file(profileImage)
.file(backgroundImage)
).andExpect(status().isOk());
var argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(profileService, times(1)).uploadProfileImages(argumentCaptor.capture());
var result = (List<MultipartFile>) argumentCaptor.getValue();
assertEquals(2, result.size());
assertEquals("profile.jpg", result.get(0).getOriginalFilename());
assertEquals("image/jpeg", result.get(0).getContentType());
assertArrayEquals("profile-image-binary".getBytes(), result.get(0).getBytes());
assertEquals("background.jpg", result.get(1).getOriginalFilename());
assertEquals("image/jpeg", result.get(1).getContentType());
assertArrayEquals("background-image-binary".getBytes(), result.get(1).getBytes());
}
}
2.4. Test uploadProfile Method
uploadProfile 메서드를 테스트한다.
- 더미(dummy) MultipartFile 인스턴스를 생성한다.
- 파라미터 키는
picture이다. - Profile 클래스에 포함된 파일 속성의 이름이
picture이다.
- 파라미터 키는
- 요청 파라미터로 이름, 나이 정보를 전달한다.
- DTO 클래스로 파일을 받는 경우 요청 파라미터를 통해 값들이 전달된다.
- 컨텐츠 타입이
application/json이 아닌 경우 스프링 프레임워크는 해당 데이터를 요청 파라미터로 받는다.
- profileService 스파이(spy) 객체에 전달된 파라미터 값을 확인한다.
- Profile 인스턴스에 포함된 이름, 나이, 사진 정보를 확인한다.
package action.in.blog.controller;
import action.in.blog.domain.Profile;
import action.in.blog.service.ProfileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class ProfileControllerTest {
ProfileService profileService;
MockMvc sut;
@BeforeEach
void setUp() {
profileService = Mockito.mock(ProfileService.class);
sut = MockMvcBuilders
.standaloneSetup(new ProfileController(profileService))
.build();
}
@Test
public void uploadProfile() throws Exception {
var profileImage = new MockMultipartFile("picture", "profile.jpg", "image/jpeg", "profile-image-binary".getBytes());
sut.perform(
multipart("/profiles")
.file(profileImage)
.param("name", "junhyunny")
.param("age", "20")
).andExpect(status().isOk());
var argumentCaptor = ArgumentCaptor.forClass(Profile.class);
verify(profileService, times(1)).uploadProfile(argumentCaptor.capture());
var result = argumentCaptor.getValue();
assertEquals("junhyunny", result.name());
assertEquals(20, result.age());
assertEquals("profile.jpg", result.picture().getOriginalFilename());
assertEquals("image/jpeg", result.picture().getContentType());
assertArrayEquals("profile-image-binary".getBytes(), result.picture().getBytes());
}
}
TEST CODE REPOSITORY
REFERENCE
광고
KEEP READING
함께 읽으면 좋은 글
DISCUSSION