diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java b/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java new file mode 100644 index 0000000000..d624ea368f --- /dev/null +++ b/spring-mvc-java-2/src/main/java/com/baeldung/multiparttesting/MultipartPostRequestController.java @@ -0,0 +1,17 @@ +package com.baeldung.multiparttesting; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RestController +public class MultipartPostRequestController { + + @PostMapping(path = "/upload") + public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { + return file.isEmpty() ? new ResponseEntity(HttpStatus.NOT_FOUND) : new ResponseEntity(HttpStatus.OK); + } +} \ No newline at end of file diff --git a/spring-mvc-java-2/src/test/java/com/baeldung/multiparttesting/MultipartPostRequestUnitTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/multiparttesting/MultipartPostRequestUnitTest.java new file mode 100644 index 0000000000..2e88935c1b --- /dev/null +++ b/spring-mvc-java-2/src/test/java/com/baeldung/multiparttesting/MultipartPostRequestUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.multiparttesting; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.matrix.config.MatrixWebConfig; + +@WebAppConfiguration +@ContextConfiguration(classes = { MatrixWebConfig.class, MultipartPostRequestController.class }) +@RunWith(SpringJUnit4ClassRunner.class) +public class MultipartPostRequestUnitTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + @Test + public void whenFileUploaded_thenVerifyStatus() throws Exception { + MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE, "Hello, World!".getBytes()); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + mockMvc.perform(multipart("/upload").file(file)).andExpect(status().isOk()); + } +} \ No newline at end of file