所以我有一个方法,需要和用户Id和multipartFile并将其上传到aws,我试图测试它,但我得到和错误java.lang.IllegalArgumentException:没有足够的变量值可用于扩展
这是上传文件的方法
public void uploadUserProfileImage(@PathVariable("userProfileId")UUID userProfileId ,
@RequestParam("file")MultipartFile file){
userProfileService.uploadUserProfileImage(userProfileId,file);
}
这是我想出来的测试,我试着学习如何测试我的方法。
@Test
void uploadUserProfileImage() throws Exception {
BufferedImage img;
img = ImageIO.read(new File("src/main/java/images/bane.png"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(img, "file", byteArrayOutputStream);
byte[] byteArryaImg = byteArrayOutputStream.toByteArray();
//given
MockMultipartFile file = new MockMultipartFile("file",
"file",
MediaType.IMAGE_PNG_VALUE,
byteArryaImg);
UserProfileModel user = new UserProfileModel(UUID.randomUUID(), "luad", "ada");
String userProfileId = user.getUserProfileId().toString();
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(multipart("/{userProfileId}/image/upload")
.file(file)
.accept(MediaType.IMAGE_PNG))
.andExpect(status()
.isOk());
}
我想你可以使用下面的语句;
MockMultipartFile mockMultipartFile = new MockMultipartFile("image-name", "mock-file.png", "image/jpeg", "image".getBytes());
MockHttpServletRequestBuilder mockRequest = fileUpload("/{userProfileId}/image/upload").file(mockMultipartFile)
你需要从;
导入fileUpluad方法import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
则只需执行;
mockMvc.perform(mockRequest).andExpect(status().isOk());