我有一个集成测试,它在我的控制器上发出请求(上传文件)。该测试无需设置任何 CommonsMultipartResolver 即可工作。但是在我必须设置生产环境的那一刻,我必须添加 CommonsMultipartResolver。但这有副作用,我的测试不起作用。同样,生产需要xml配置,而不是测试。我知道可以为测试和生产环境定义配置文件。还有没有其他可能性没有个人资料?
multipartresolver 的配置很简单:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxUploadSize="1000000000">
</bean>
我的测试也很简单:
MockMultipartFile aFileObject = new MockMultipartFile("file", "filename.txt", "text/plain", "a File message".getBytes());
HashMap<String, String> contentTypeParams = new HashMap<String, String>();
contentTypeParams.put("boundary", "xyz");
MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams);
MockHttpServletRequestBuilder action = fileUpload(path).file(aFileObject));
mockMvc=MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(new DelegatingFilterProxy("springSecurityFilterChain", webApplicationContext), "/*")
.build()
ResultActions resultPost =mockMvc.perform(action.contentType(mediaType));
assertThat(.....
(我已经简化了一点测试代码(这不是这里的问题。它有效。))
有谁知道如何在测试运行时弄清楚 Multipartresolver 的配置,并在我将所有内容投入生产时启用,而我每次都必须记住评论配置?