Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MultipartFile#getContentType在Spring的集成测试期间返回null_Java_Spring_Integration Testing_Spring Test - Fatal编程技术网

Java MultipartFile#getContentType在Spring的集成测试期间返回null

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

我有以下文件:

ImageForm.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));
    }
}
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已创建
    我设置了一个文件
  • 测试将文件发送到GalleryController#createImage并映射它 到
    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
    尝试不创建或声明一些附加值或参数,如果您没有声明它们的值或参数。

    谢谢,这就是问题所在。