Java ';未找到多部分边界';使用CommonsMultipartResolver测试最大上载大小时

Java ';未找到多部分边界';使用CommonsMultipartResolver测试最大上载大小时,java,spring,unit-testing,file-upload,Java,Spring,Unit Testing,File Upload,在实际应用程序中,我使用的是org.springframework.web.multipart.commons.commonmultipartresolver,其中我定义了maxUploadSize参数。因此,我不想使用SpringMVC测试来测试捕获MaxUploadSizeExceedeException 为此,我在Java中添加了org.springframework.web.multipart.support.MultipartFilter: @Autowired private Web

在实际应用程序中,我使用的是org.springframework.web.multipart.commons.commonmultipartresolver,其中我定义了maxUploadSize参数。因此,我不想使用SpringMVC测试来测试捕获
MaxUploadSizeExceedeException

为此,我在Java中添加了
org.springframework.web.multipart.support.MultipartFilter

@Autowired
private WebApplicationContext wac;

@Autowired
private MultipartFilter multipartFilter;

private MockMvc mockMvc;

@Before
public void init() {
    mockMvc = MockMvcBuilders.webAppContextSetup(wac)
        .addFilter(multipartFilter).build();
}
在测试上下文(基于xml的配置)中:

运行i get时,由以下原因引起:org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为在org.apache.commons.fileupload.FileUploadBase中未找到多部分边界

据我所知,在现实生活中,浏览器在内容类型头中设置唯一的边界,并用该边界分隔身体中的各个部分,但spring模拟测试在发送模拟多部分请求时不设置边界

因此,我找到了
org.springframework.http.converter.FormHttpMessageConverter
,创建
org.springframework.test.web.servlet.request.RequestPostProcessor
,其中将正文和内容类型头转换为正确(带边界),并将后处理器添加到请求生成器-测试通过

也许有人知道更好或更简单的方法来为模拟请求的内容类型头和主体添加边界

PS
如果没有更好的方法-我可以将我的RequestPostProcessor附加到此处
使用版本:

  • 弹簧试验-3.2.0
  • commons-fileupload-1.2.2

我找到了一种测试MaxUploadSizeExceedeException的干净方法

它涉及到使用Mockito监视MultipartResolver以强制抛出异常

  • 使用一个内部@Configuration类,连接一个Mockito间谍的多部分解析器
  • 将解析器自动关联到类中
  • 对于MaxUploadSizeExceedeException测试用例,存根解析程序以引发异常,然后使用内容类型MediaType.MULTIPART\u FORM\u数据执行post
这是密码

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SpringMVCConfiguration.class, UploadTest.TestConfiguration.class})
@SuppressWarnings("unchecked")
public final class UploadTest {

    public static final String MERCHANT_AUTH = "MERCHANT_AUTH";
    public static final String USER = "user";

    @Configuration
    public static class TestConfiguration {
        @Bean
        public MultipartResolver multipartResolver() {
            CommonsMultipartResolver resolver = new CommonsMultipartResolver();
            resolver.setMaxUploadSize(500 * 1028);
            return Mockito.spy(resolver);
        }
    }

    @Autowired
    private WebApplicationContext m_webApplicationContext;

    @Autowired
    private CommonsMultipartResolver m_multipartResolver;

    private MockMvc m_mockMvc;

    @Before
    public void setUp() throws Exception {
        m_mockMvc = MockMvcBuilders.webAppContextSetup(m_webApplicationContext).build();
    }

    @Test
    public void testName() throws Exception {
        Mockito.reset(m_multipartResolver);
    }

    @Test
    public void testUpload_FileTooLarge() throws Exception {

        /*
        Force multipart resolver to throw file too large exception
        */
        doThrow(new MaxUploadSizeExceededException(100)).when(m_multipartResolver).resolveMultipart(
            any(HttpServletRequest.class));

        /*
            Do a multipart post. Cannot use fileUpload as that skips calling the MultipartResolver
         */
        m_mockMvc.perform(post("/upload/")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isRequestEntityTooLarge())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

    }
}    

我找到了一种测试MaxUploadSizeExceedeException的干净方法

它涉及到使用Mockito监视MultipartResolver以强制抛出异常

  • 使用一个内部@Configuration类,连接一个Mockito间谍的多部分解析器
  • 将解析器自动关联到类中
  • 对于MaxUploadSizeExceedeException测试用例,存根解析程序以引发异常,然后使用内容类型MediaType.MULTIPART\u FORM\u数据执行post
这是密码

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SpringMVCConfiguration.class, UploadTest.TestConfiguration.class})
@SuppressWarnings("unchecked")
public final class UploadTest {

    public static final String MERCHANT_AUTH = "MERCHANT_AUTH";
    public static final String USER = "user";

    @Configuration
    public static class TestConfiguration {
        @Bean
        public MultipartResolver multipartResolver() {
            CommonsMultipartResolver resolver = new CommonsMultipartResolver();
            resolver.setMaxUploadSize(500 * 1028);
            return Mockito.spy(resolver);
        }
    }

    @Autowired
    private WebApplicationContext m_webApplicationContext;

    @Autowired
    private CommonsMultipartResolver m_multipartResolver;

    private MockMvc m_mockMvc;

    @Before
    public void setUp() throws Exception {
        m_mockMvc = MockMvcBuilders.webAppContextSetup(m_webApplicationContext).build();
    }

    @Test
    public void testName() throws Exception {
        Mockito.reset(m_multipartResolver);
    }

    @Test
    public void testUpload_FileTooLarge() throws Exception {

        /*
        Force multipart resolver to throw file too large exception
        */
        doThrow(new MaxUploadSizeExceededException(100)).when(m_multipartResolver).resolveMultipart(
            any(HttpServletRequest.class));

        /*
            Do a multipart post. Cannot use fileUpload as that skips calling the MultipartResolver
         */
        m_mockMvc.perform(post("/upload/")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isRequestEntityTooLarge())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

    }
}    

嗨,你能把你的请求后处理器放在这里吗?这将非常有帮助,非常感谢@user2628641不幸的是我丢失了这个旧项目(嗨,你能在这里展示你的请求后处理器吗?它会非常有用,非常感谢!@user2628641不幸的是我丢失了这个旧项目(
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SpringMVCConfiguration.class, UploadTest.TestConfiguration.class})
@SuppressWarnings("unchecked")
public final class UploadTest {

    public static final String MERCHANT_AUTH = "MERCHANT_AUTH";
    public static final String USER = "user";

    @Configuration
    public static class TestConfiguration {
        @Bean
        public MultipartResolver multipartResolver() {
            CommonsMultipartResolver resolver = new CommonsMultipartResolver();
            resolver.setMaxUploadSize(500 * 1028);
            return Mockito.spy(resolver);
        }
    }

    @Autowired
    private WebApplicationContext m_webApplicationContext;

    @Autowired
    private CommonsMultipartResolver m_multipartResolver;

    private MockMvc m_mockMvc;

    @Before
    public void setUp() throws Exception {
        m_mockMvc = MockMvcBuilders.webAppContextSetup(m_webApplicationContext).build();
    }

    @Test
    public void testName() throws Exception {
        Mockito.reset(m_multipartResolver);
    }

    @Test
    public void testUpload_FileTooLarge() throws Exception {

        /*
        Force multipart resolver to throw file too large exception
        */
        doThrow(new MaxUploadSizeExceededException(100)).when(m_multipartResolver).resolveMultipart(
            any(HttpServletRequest.class));

        /*
            Do a multipart post. Cannot use fileUpload as that skips calling the MultipartResolver
         */
        m_mockMvc.perform(post("/upload/")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isRequestEntityTooLarge())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

    }
}