쌓고 쌓다
테스트 코드에 MultipartFile 사용하는 방법 (파일 테스트 코드) 본문
반응형
테스트 코드를 작성하며 실제로 업로드한 파일이 서버와 디스크에 저장되는지 확인할 필요가 있었다.
다음 메서드를 호출하여 정상적으로 파일이 저장되길 원한다.
public Location addLocation(Location location, List<MultipartFile> files)
List<MultipartFile>은 어떻게 생성해서 넘거야할까?
다음과 같이 MockMultipartFile을 사용하여 MultipartFile을 대체할 수 있다.
String imgName1 = "abc1.jpg";
String imgName2 = "abc2.jpg";
MockMultipartFile file1 = new MockMultipartFile(imgName1, imgName1, MediaType.IMAGE_JPEG_VALUE, "img".getBytes());
MockMultipartFile file2 = new MockMultipartFile(imgName2, imgName2, MediaType.IMAGE_JPEG_VALUE, "img".getBytes());
List<MultipartFile> files = new ArrayList<>();
files.add(file1);
files.add(file2);
MockMultipartFile의 생성자는 다음과 같다.

addLocation 메서드에는 MockMultipartFile을 통해 파일을 생성해서 저장하는 로직이 포함되어 있다.
파일이 생성되었는지 확인하고 삭제하는 과정은 File을 통해 경로를 넣어
exists, delete 메서드를 활용할 수 있다.
File file = new File("경로");
if(file.exists())
file.delete();
else
throw new FileNotFoundException();
장소 등록시 함께 넘긴 이미지 파일은
파일 생성이 되었는지 확인하고 삭제하는 과정을 테스트 코드에 포함했다.
@Test
@DisplayName("Location 등록")
void addLocation() throws IOException {
//given
String title = "테스트 장소";
Double longitude = 1.1;
Double latitude = 2.2;
String address = "주소";
String description = "설명";
Location location = new Location();
location.setTitle(title);
location.setLongitude(longitude);
location.setLatitude(latitude);
location.setAddress(address);
location.setDescription(description);
String imgName1 = "abc1.jpg";
String imgName2 = "abc2.jpg";
MockMultipartFile file1 = new MockMultipartFile(imgName1, imgName1, MediaType.IMAGE_JPEG_VALUE, "img".getBytes());
MockMultipartFile file2 = new MockMultipartFile(imgName2, imgName2, MediaType.IMAGE_JPEG_VALUE, "img".getBytes());
List<MultipartFile> files = new ArrayList<>();
files.add(file1);
files.add(file2);
//when
locationService.addLocation(location, files);
//then
Location findLocation = locationRepository.findById(location.getId())
.orElseThrow(() -> new NoSuchElementException());
List<LocationImage> locationImages = locationImageRepository.findByLocationId(findLocation.getId());
Assertions
.assertThat(findLocation.getTitle())
.isEqualTo(title);
Assertions
.assertThat(findLocation.getLongitude())
.isEqualTo(longitude);
Assertions
.assertThat(findLocation.getLatitude())
.isEqualTo(latitude);
Assertions
.assertThat(findLocation.getAddress())
.isEqualTo(address);
Assertions
.assertThat(findLocation.getDescription())
.isEqualTo(description);
Assertions
.assertThat(locationImages.size())
.isEqualTo(2);
for (LocationImage locationImage : locationImages) {
File file = new File(imageStore.getLocationImgFullPath(locationImage.getStoreFileName()));
if(file.exists())
file.delete();
else
throw new FileNotFoundException();
}
}반응형
'Dev Log' 카테고리의 다른 글
| [프로그래머스] 가장 많이 받은 선물 C++ 풀이 및 해설 (1) | 2024.03.16 |
|---|---|
| JSON Array를 읽고 원하는 클래스로 변환하는 방법 (0) | 2024.03.15 |
| [프로그래머스] 도넛과 막대 그래프 C++ 풀이 및 해설 (0) | 2024.03.09 |
| flush와 clear의 차이 (테스트 코드에서 SELECT가 안나가는 이유) (0) | 2024.03.08 |
| Entity의 List 필드(컬렉션)는 초기화 해주자 (0) | 2024.03.08 |