在实际应用程序中,我正在使用org.springframework.web.multipart.commons.CommonsMultipartResolver
,其中我定义 maxUploadSize 参数。所以我想尝试使用spring mvc test来测试MaxUploadSizeExceededException
。
为此我在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();
}
在测试conext(基于xml的配置)中:
<bean id="multipartFilter" class="org.springframework.web.multipart.support.MultipartFilter">
<property name="multipartResolverBeanName" value="MyMultipartResolverBeanName" />
</bean>
MyMultipartResolverBeanName bean在现实生活中定义,使用maven-surefire-plugin添加到测试上下文中。
我的测试就像在教程中一样:
@Test(expected = MaxUploadSizeExceededException.class)
public void testCertSizeInvalid() throws Exception {
mockMvc.perform(fileUpload(SUBMIT_URL)
.file(new MockMultipartFile("fileName", new byte[MAX_UPLOAD_SIZE + 10]))
);
}
运行时遇到引起:org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界来自org.apache.commons.fileupload.FileUploadBase
据我所知,在现实生活中,浏览器在Content-Type标题中设置了唯一的边界,并用这个边界分隔了正文中的部分,但是spring模拟测试并没有设置发送模拟多部分请求的边界文件。
所以我找到org.springframework.http.converter.FormHttpMessageConverter
,创建org.springframework.test.web.servlet.request.RequestPostProcessor
,其中将body和Content-Type标头转换为正确(带边界)并将post处理器添加到请求构建器 - 测试通过!
也许有人知道更好或更简单的方法来为Content-Type标题和模拟请求的主体添加边界?!
PS
如果没有更好的方法 - 我可以在这里附上我的RequestPostProcessor
二手版本:
答案 0 :(得分:1)
我找到了一种很好的方法来测试MaxUploadSizeExceededException
它涉及使用Mockito监视MultipartResolver以强制抛出异常。
这是代码。
@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));
}
}