De2o freelancer (OCR)

Mocmvc file 포함 parameter 전송

이무쿤 2020. 9. 25. 16:32
반응형

<기존>

 @Test
	  public void create() throws Exception {
	      final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
	      params.add("registerUserId", "12");
	      params.add("changeUserId", "12");
	      params.add("categoryName", "Junit create test");
	      params.add("description", "Junit create test");
	      params.add("parentCategoryId", "1");
	      params.add("categoryLevel", "1");
	        mockMvc.perform(MockMvcRequestBuilders.post("/v1/category")
	              // .header("X-AUTH-TOKEN", token)
	              .params(params)).andExpect(status().isOk()).andDo(print())
	        	  .andExpect(jsonPath("$.success").value(true));
	        	  
	  }

 

 

<file 추가>

public void create() throws Exception {
	      final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
	      MockMultipartFile file = new MockMultipartFile("file","type_A.jpeg" , "image/jpeg" , "hello file".getBytes());

	      
	      params.add("registerUserId", "9999");
	       params.add("changeUserId", "9999");
	       params.add("imageTypeCd", "1");
	       params.add("productId", "1");
	        mockMvc.perform(multipart("/v1/product/image").file(file)
	              // .header("X-AUTH-TOKEN", token)
	              .params(params)).andExpect(status().isOk()).andDo(print())
	        	  .andExpect(jsonPath("$.success").value(true));
	        	  
	  }

 

 [file이 전달되는 파라미터 명] , [파일 실제이름] , [파일 타입] , [변환되는 파일 이름이므로 신경 x]

 MockMultipartFile file = new MockMultipartFile("file","type_A.jpeg" , "image/jpeg" , "hello file".getBytes());
반응형