Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Process
- Thread Multiplexing
- Spring
- task_struct
- spring-data-jpa
- sql-mappler
- python-socketio
- Thread Library
- @Header
- AOP
- 도커
- OS
- 문자열 리터럴
- rainbow table
- 문자열 불변성
- 함수형 인터페이스
- functional interface
- 쓰레드 라이브러리
- FunctionalInterface
- 운영체제
- 롬복주의점
- django-crontab
- strict stubbing
- hiberbate
- 프로세스
- SystemCall
- java.util.function
- custom annotation
- ReflectUtils
- none이미지
Archives
- Today
- Total
JH's Develog
[Spring] Mockmvc로 multipart/form-data 테스트하기 본문
Postman에서 위와 같은 형식으로 multipart/form-data 요청을 보내는 것과 같은 형식으로 Controller 테스트를 작성해보겠습니다.
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
...
@AutoConfigureMockMvc
@SpringBootTest
public class NoteControllerTest {
@Autowired private MockMvc mvc;
@Test
void test() throws Exception{
//Given
final String fileName = "testImage1"; //파일명
final String contentType = "png"; //파일타입
final String filePath = "src/test/resources/testImage/"+fileName+"."+contentType; //파일경로
FileInputStream fileInputStream = new FileInputStream(path);
//Mock파일생성
MockMultipartFile image1 = new MockMultipartFile(
"images", //name
fileName + "." + contentType, //originalFilename
contentType,
fileInputStream
);
MockMultipartFile image2 = new MockMultipartFile(
"images", //name
fileName + "." + contentType, //originalFilename
contentType,
fileInputStream
);
//When & Then
mvc.perform(
multipart("/api/note")
.file(image1).file(image2)
.param("title", "제목1")
.param("description", "설명설명")
).andExpect(status().isOk();
}
MockMultipartFile 객체 생성은 다양한 방법이 있는데 그중 InputStream을 사용한 방식은 아래와 같습니다.
- name : 파일의 이름 (실제 파일의 이름이 아니라 POST 요청에서 해당 파일을 Value로 가지는 Key의 이름입니다)
- originalFilename : 원래 파일의 이름입니다.
- contentType : jpg, png 같은 파일의 형식입니다.
- contentStream : 파일경로로 생성한 InputStream입니다.
+참고
ReflectionUtils를 활용해 파라미터가 많은 Multipart 요청을 좀 더 유지보수하기 쉽게 만드는 방법입니다.
https://jhkimmm.tistory.com/32
[Spring] ReflectUtils로 클래스 멤버에 접근하기 + 테스트 코드 리팩토링에 적용
ReflectUtils란? ReflectUtils는 Spring에서 제공하는 유틸리티 클래스이며, Java Bean 형식의 클래스에 대해서 자바의 Reflection API를 쉽게 사용할 수 있게 도와줍니다. ReflectUtils를 사용하면 자바의 Reflec..
jhkimmm.tistory.com
'Spring' 카테고리의 다른 글
[Spring] ReflectUtils로 클래스 멤버에 접근하기 + 테스트 코드 리팩토링에 적용 (0) | 2022.06.01 |
---|---|
[Spring Security] Filter 에서 발생한 예외 핸들링하기 (1) | 2022.05.20 |
[Spring] Mockito의 Strict Stubbing과 lenient의 차이는? (0) | 2022.05.12 |
[Spring Security] HandlerMethodArgumentResolver로 Authentication에서 Principal 정보 가져오기 (0) | 2022.04.14 |
[SpringBoot] Entity 속성정보 상속받기 (0) | 2022.04.07 |
Comments