У меня есть следующие файлы:
ImageForm.java:
public class ImageForm {
@FileNotEmpty
private MultipartFile file;
// other code ...
}
GalleryController.java:
@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 ...
}
}
GalleryControllerIT.java:
@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_ImageIsCreated
Я установил файл. - Тест отправляет файл на
GalleryController#createImage
и сопоставляет его с атрибутом ImageForm#file
. ImageForm#file
имеет тип MultipartFile
, который имеет метод getContentType
. - Метод
MultipartFile#getContentType
возвращает null
.
Вопрос в том, почему MultipartFile#getContentType
возвращает ноль?Он работает правильно, когда за пределами теста.