Skip to content

Commit 365a42d

Browse files
committed
Add file upload test to Spring MVC Test
1 parent dc01f08 commit 365a42d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

spring-test-mvc/src/main/java/org/springframework/test/web/mock/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ protected MockHttpServletRequest createServletRequest(ServletContext servletCont
636636
createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext);
637637
}
638638

639-
private static MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
639+
private MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
640640
try {
641641
String className = "org.springframework.test.web.mock.servlet.request.Servlet3MockHttpServletRequest";
642642
Class<?> clazz = ClassUtils.forName(className, MockHttpServletRequestBuilder.class.getClassLoader());

spring-test-mvc/src/main/java/org/springframework/test/web/mock/servlet/request/MockMultipartHttpServletRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ protected final MockHttpServletRequest createServletRequest(ServletContext servl
108108
return request;
109109
}
110110

111-
private static MockMultipartHttpServletRequest createServlet3Request() {
111+
private MockMultipartHttpServletRequest createServlet3Request() {
112112
try {
113113
String className = "org.springframework.test.web.mock.servlet.request.Servlet3MockMultipartHttpServletRequest";
114114
Class<?> clazz = ClassUtils.forName(className, MockMultipartHttpServletRequestBuilder.class.getClassLoader());
115-
Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
115+
Constructor<?> constructor = clazz.getDeclaredConstructor();
116116
return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);
117117
}
118118
catch (Throwable t) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.test.web.mock.servlet.samples.standalone;
17+
18+
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.fileUpload;
19+
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.model;
20+
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup;
21+
22+
import java.io.IOException;
23+
24+
import org.junit.Test;
25+
import org.springframework.mock.web.MockMultipartFile;
26+
import org.springframework.stereotype.Controller;
27+
import org.springframework.ui.Model;
28+
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.bind.annotation.RequestMethod;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.multipart.MultipartFile;
32+
33+
public class FileUploadControllerTests {
34+
35+
@Test
36+
public void readString() throws Exception {
37+
38+
MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes());
39+
40+
standaloneSetup(new FileUploadController()).build()
41+
.perform(fileUpload("/fileupload").file(file))
42+
.andExpect(model().attribute("message", "File 'orig' uploaded successfully"));
43+
}
44+
45+
46+
@Controller
47+
private static class FileUploadController {
48+
49+
@RequestMapping(value="/fileupload", method=RequestMethod.POST)
50+
public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException {
51+
model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
52+
return "redirect:/index";
53+
}
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)