This example shows how to unit test a Spring file upload controller using MockMultipartFile.
MockMultipartFile bypasses any application-registered MultipartResolver. This means it is only suitable for unit testing controller methods that directly handle multipart upload data.
Example
The Controller
@Controller
@RequestMapping("/upload")
public class FileUploadController {
public static final String TARGET_FOLDER = "d:/filesUploaded/";
@PostMapping
@ResponseBody
public String handlePostRequest(MultipartHttpServletRequest request) throws IOException {
MultipartFile multipartFile = request.getFile("user-file");
String name = multipartFile.getOriginalFilename();
InputStream inputStream = multipartFile.getInputStream();
Files.copy(inputStream,
Paths.get(TARGET_FOLDER + name),
StandardCopyOption.REPLACE_EXISTING);
return "test response body";
}
}
Java Config
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
}
The Test class
@SpringJUnitWebConfig(MyWebConfig.class)
public class UploadControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvcTester mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = MockMvcTester.from(this.wac);
}
@Test
public void testController() throws Exception {
String fileName = "test.txt";
File file = new File(FileUploadController.TARGET_FOLDER + fileName);
file.delete();
MockMultipartFile mockMultipartFile =
new MockMultipartFile("user-file", fileName,
"text/plain",
"test data".getBytes());
assertThat(
mockMvc.perform(
multipart("/upload")
.file(mockMultipartFile)
)
).hasStatusOk();
assertThat(file).exists();
assertThat(Files.readAllBytes(file.toPath()))
.asString()
.isEqualTo("test data");
}
}
mvn test -Dtest="UploadControllerTest" Output$ mvn test -Dtest="UploadControllerTest" [INFO] Scanning for projects... [INFO] [INFO] ------< com.logicbig.example:spring-mock-multipart-file-example >------- [INFO] Building spring-mock-multipart-file-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ spring-mock-multipart-file-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\file-upload\spring-mock-multipart-file-example\src\main\resources [INFO] [INFO] --- compiler:3.15.0:compile (default-compile) @ spring-mock-multipart-file-example --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-mock-multipart-file-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\file-upload\spring-mock-multipart-file-example\src\test\resources [INFO] [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ spring-mock-multipart-file-example --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ spring-mock-multipart-file-example --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.UploadControllerTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.640 s -- in com.logicbig.example.UploadControllerTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.098 s [INFO] Finished at: 2026-06-07T15:02:57+08:00 [INFO] ------------------------------------------------------------------------
Note:
MockMvcRequestBuilders.multipart() replaced MockMvcRequestBuilders.fileUpload() in Spring 5. The old method was removed in Spring 6. Check out this example using fileUpload().
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- spring-test 7.0.6 (Spring TestContext Framework)
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- junit-jupiter-engine 6.0.3 (Module "junit-jupiter-engine" of JUnit)
- assertj-core 3.26.3 (Rich and fluent assertions for testing in Java)
- JDK 25
- Maven 3.9.11
|