How to use spring's MockMultipartHttpServletRequest? Getting “no multipart boundary was found”

前端 未结 5 449
名媛妹妹
名媛妹妹 2021-01-02 10:53

Clearly I\'m not using this test fixture right. My servlet works just fine in tomcat, but when I try to use this mock, the multi-part boundary is not found. \"the request

5条回答
  •  轮回少年
    2021-01-02 11:34

    My way to test multipart/form-data via mock MVC sending params and files is the following:

        @Test
    void testSendFeedback() throws Exception {
        var builder = MockMvcRequestBuilders.multipart(URL_PATH);
    
        Path path = Files.createTempFile("test-file", "tmp");
        builder = builder.part(new MockPart("image", path.toFile().getName(), Files.readAllBytes(path)));
    
        builder.param("field1", "value1")
            .param("fields2", "value2");
    
        mockMvc.perform(builder.header(HttpHeaders.AUTHORIZATION, YOUR_AUTH_VALUE).contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
            .andDo(print())
            .andExpect(status().isNoContent());
    }
    

提交回复
热议问题