我有一個集成測試,它在我的控制器上發出請求(上傳文件)。 測試的工作原理沒有設置任何CommonsMultipartResolver。但是在必須設置生產環境的那一刻,我必須添加CommonsMultipartResolver。但是這有副作用,我的測試不起作用。 這與產品需要xml配置相同,而不是測試。 我知道有可能爲測試和生產環境定義配置文件。有沒有其他的可能性沒有配置文件?在Spring中使用CommonsMultipartResolver並在測試中配置
的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的配置,那麼我必須記住每次評論配置?
嗨,txs是你的時間。我喜歡答案,我看到了解決方案。我試圖將示例中的測試適用於我的案例。控制器工作。我必須弄清楚我的情況下授權的其他問題。發送很多。 – java4fun
你有這個問題的解決方案http://stackoverflow.com/questions/32293550/unit-test-of-file-upload-using-mockmvcbuilders-with-standalone-context-and-sprin/32294564? – JeanValjean