Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestController
- @RequestMapping("file-upload")
- public class MyRESTController {
- @Autowired
- private AService aService;
- @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
- @ResponseStatus(HttpStatus.CREATED)
- public void fileUpload(
- @RequestParam(value = "file", required = true) final MultipartFile file,
- @RequestParam(value = "something", required = true) final String something) {
- aService.doSomethingOnDBWith(file, value);
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = MyApplication.class)
- @WebAppConfiguration
- public class ControllerTest{
- MockMvc mockMvc;
- @Mock
- AService aService;
- @InjectMocks
- MyRESTController controller;
- @Before public void setUp(){
- MockitoAnnotations.initMocks(this);
- this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
- }
- @Test
- public void testFileUpload() throws Exception{
- final File file = getFileFromResource(fileName);
- //File is correctly loaded
- final MockMultipartFile multipartFile = new MockMultipartFile("aMultiPartFile.txt", new FileInputStream(file));
- doNothing().when(aService).doSomethingOnDBWith(any(MultipartFile.class), any(String.class));
- mockMvc.perform(
- post("/file-upload")
- .requestAttr("file", multipartFile.getBytes())
- .requestAttr("something", ":(")
- .contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
- .andExpect(status().isCreated());
- }
- }
- java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement