Java MultipartFile#getContentType在Spring的集成测试期间返回null
我有以下文件: ImageForm.java:Java MultipartFile#getContentType在Spring的集成测试期间返回null,java,spring,integration-testing,spring-test,Java,Spring,Integration Testing,Spring Test,我有以下文件: ImageForm.java: public class ImageForm { @FileNotEmpty private MultipartFile file; // other code ... } @Controller @RequestMapping("/admin/galleries") public class GalleryController { @PostMapping("/{id}/image/create") public ModelAn
public class ImageForm {
@FileNotEmpty
private MultipartFile file;
// other code ...
}
@Controller
@RequestMapping("/admin/galleries")
public class GalleryController {
@PostMapping("/{id}/image/create")
public ModelAndView createImage(@PathVariable("id") long galleryId, @Valid ImageForm imageForm, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
// other code ...
System.out.println(imageForm.getFile().getContentType()); // Prints: null
// other code ...
}
}
@SqlGroup({
@Sql("classpath:test-schema.sql"),
@Sql("classpath:test-gallery-data.sql"),
@Sql("classpath:test-image-data.sql")
})
public class GalleryControllerIT extends SetupControllerIT {
@Test
public void createImage_POSTHttpMethod_ImageIsCreated() throws Exception {
Path path = Paths.get(getClass().getClassLoader().getResource("test-image.png").toURI());
byte[] image = Files.readAllBytes(path);
mvc.perform(
multipart("/admin/galleries/1/image/create")
.file("file", image) // TODO: ImageForm.file.contentType is null.
.with(csrf())
).andExpect(status().isFound());
assertThat(imageRepository.count(), is(5L));
}
}
GalleryController.java:
public class ImageForm {
@FileNotEmpty
private MultipartFile file;
// other code ...
}
@Controller
@RequestMapping("/admin/galleries")
public class GalleryController {
@PostMapping("/{id}/image/create")
public ModelAndView createImage(@PathVariable("id") long galleryId, @Valid ImageForm imageForm, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
// other code ...
System.out.println(imageForm.getFile().getContentType()); // Prints: null
// other code ...
}
}
@SqlGroup({
@Sql("classpath:test-schema.sql"),
@Sql("classpath:test-gallery-data.sql"),
@Sql("classpath:test-image-data.sql")
})
public class GalleryControllerIT extends SetupControllerIT {
@Test
public void createImage_POSTHttpMethod_ImageIsCreated() throws Exception {
Path path = Paths.get(getClass().getClassLoader().getResource("test-image.png").toURI());
byte[] image = Files.readAllBytes(path);
mvc.perform(
multipart("/admin/galleries/1/image/create")
.file("file", image) // TODO: ImageForm.file.contentType is null.
.with(csrf())
).andExpect(status().isFound());
assertThat(imageRepository.count(), is(5L));
}
}
GalleryControllerIT.java:
public class ImageForm {
@FileNotEmpty
private MultipartFile file;
// other code ...
}
@Controller
@RequestMapping("/admin/galleries")
public class GalleryController {
@PostMapping("/{id}/image/create")
public ModelAndView createImage(@PathVariable("id") long galleryId, @Valid ImageForm imageForm, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
// other code ...
System.out.println(imageForm.getFile().getContentType()); // Prints: null
// other code ...
}
}
@SqlGroup({
@Sql("classpath:test-schema.sql"),
@Sql("classpath:test-gallery-data.sql"),
@Sql("classpath:test-image-data.sql")
})
public class GalleryControllerIT extends SetupControllerIT {
@Test
public void createImage_POSTHttpMethod_ImageIsCreated() throws Exception {
Path path = Paths.get(getClass().getClassLoader().getResource("test-image.png").toURI());
byte[] image = Files.readAllBytes(path);
mvc.perform(
multipart("/admin/galleries/1/image/create")
.file("file", image) // TODO: ImageForm.file.contentType is null.
.with(csrf())
).andExpect(status().isFound());
assertThat(imageRepository.count(), is(5L));
}
}
GallerControllerIT#createImage_POSTHttpMethod_Images已创建
我设置了一个文件李>
ImageForm#file
属性ImageForm#file
的类型为MultipartFile
,其方法为getContentType
李>
MultipartFile#getContentType
返回null
问题是,
MultipartFile#getContentType
为什么返回null?在测试之外时,它可以正常工作。有关完整数据,您应该调用
.file(new MockMultipartFile("image", "some_name", MediaType.MULTIPART_FORM_DATA_VALUE, image))
因为在你的例子中,如果你调用.file(“file”,image)
他们调用构造函数的短版本MockMultipartFile
,而不使用内容类型
MockMvc
尝试不创建或声明一些附加值或参数,如果您没有声明它们的值或参数。谢谢,这就是问题所在。