test post请求与spring的multipartfile

xbp102n0  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(561)

我有一个控制器发送post请求,这是工作良好,与客户沟通正确。但是,我无法在junit中测试它。控制器接收一个multipartfile和2个布尔参数(这是必需的),并且只接受“application/xls”作为内容类型。为了进行测试,我模拟了multipartfile和参数。但是,当我这样做时,会出现以下错误: handler.RestExceptionHandler: Exception caught. Wrong file type;\n Import .xls or .xlsx file (从我的异常处理程序)。

@PostMapping(value = "/file",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.TEXT_HTML_VALUE)
    public ResponseEntity<InputStreamResource> parseMultipartFile(
            @RequestParam MultipartFile multipartFile,
            @RequestParam boolean hasOnlyOneSheet,
            @RequestParam boolean hasBorders) throws IOException {

        String fileContentType = multipartFile.getContentType();
        if (mimeType.contains(fileContentType)) {

            InputStream parsedFile = multipartFileToHtmlService.multipartFileToHtml(multipartFile, hasOnlyOneSheet, hasBorders);
            InputStreamResource inputStreamResource = new InputStreamResource(parsedFile);

            return ResponseEntity.ok(inputStreamResource);
        } else {
            throw new UnsupportedImportMediaTypeException("Wrong file type;\n Import .xls or .xlsx file");
        }
    }

这是我的测试:

@WebMvcTest
@RunWith(SpringRunner.class)
class HtmlExporterControllerTest {

    @MockBean
    MultipartFileToHtmlService multipartFileToHtmlService;

    @MockBean
    HtmlToHtmlServiceFactory htmlToHtmlServiceFactory;

    @Autowired
    MockMvc mockMvc;

    @Test
   public void parseMultipartFile_Should_Return_Ok() throws Exception {
        MockMultipartFile mockMultipartFile = new MockMultipartFile(
                "multipartFile",
                "test.xls",
                "application/x-xls",
                "Hello World!".getBytes());

        mockMvc.perform(MockMvcRequestBuilders.multipart("/file")
                .file("multipartFile", mockMultipartFile.getBytes())
                .param("hasOnlyOneSheet", "true")
                .param("hasBorders", "true"))
                .andExpect(status().isOk());
    }

为了避免这个问题,我尝试将布尔值作为新的多部分/表单数据传递,如下所示:

@Test
   public void parseMultipartFile_Should_Return_Ok() throws Exception {
        MockMultipartFile mockMultipartFile = new MockMultipartFile(
                "multipartFile",
                "test.xls",
                "application/x-xls",
                "Hello World!".getBytes());

        MockMultipartFile mockMultipartFile1 = new MockMultipartFile(
                "hasBorders",
                "",
                "",
                "true".getBytes());

        MockMultipartFile mockMultipartFile2 = new MockMultipartFile(
                "hasOnlyOneSheet",
                "",
                "",
                "true".getBytes());

        mockMvc.perform(MockMvcRequestBuilders.multipart("/file")
                .file("multipartFile", mockMultipartFile.getBytes())
                .file("hasOnlyOneSheet", mockMultipartFile1.getBytes())
                .file("hasBorders", mockMultipartFile2.getBytes()))
                .andExpect(status().isOk());
    }

但是,我得到以下错误 Failed to convert value of type 'org.springframework.mock.web.MockMultipartFile' to required type 'boolean'; 同样,请求是有效的,我也可以在postman上执行,下面是curl请求:

curl --location --request POST 'http://localhost:8080/file' \
--form 'multipartFile=@/MyPath/test.xlsx' \
--form 'hasBorders=true' \
--form 'hasOnlyOneSheet=true'
pb3skfrl

pb3skfrl1#

为了防止对其他人有帮助,我添加了一个真正的excel表:

@WebMvcTest
@RunWith(SpringRunner.class)
class HtmlExporterControllerTest {

@MockBean
MultipartFileToHtmlService multipartFileToHtmlService;

@MockBean
HtmlToHtmlServiceFactory htmlToHtmlServiceFactory;

@Autowired
MockMvc mockMvc;

@Test
public void parseMultipartFile_Should_Return_Ok() throws Exception {
    MockMultipartFile mockMultipartFile = new MockMultipartFile(
            "multipartFile",
            "test.xls",
            "application/x-xls",
            new ClassPathResource("test.xls").getInputStream());

    mockMvc.perform(MockMvcRequestBuilders.multipart("/file")
            .file(mockMultipartFile)
            .param("hasOnlyOneSheet", "true")
            .param("hasBorders", "true"))
            .andExpect(status().isOk());
}

相关问题