Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. @RestController
  2. @RequestMapping("file-upload")
  3. public class MyRESTController {
  4.  
  5. @Autowired
  6. private AService aService;
  7.  
  8. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  9. @ResponseStatus(HttpStatus.CREATED)
  10. public void fileUpload(
  11. @RequestParam(value = "file", required = true) final MultipartFile file,
  12. @RequestParam(value = "something", required = true) final String something) {
  13. aService.doSomethingOnDBWith(file, value);
  14. }
  15. }
  16.  
  17. @RunWith(SpringJUnit4ClassRunner.class)
  18. @SpringApplicationConfiguration(classes = MyApplication.class)
  19. @WebAppConfiguration
  20. public class ControllerTest{
  21.  
  22. MockMvc mockMvc;
  23.  
  24. @Mock
  25. AService aService;
  26.  
  27. @InjectMocks
  28. MyRESTController controller;
  29.  
  30. @Before public void setUp(){
  31. MockitoAnnotations.initMocks(this);
  32. this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
  33. }
  34.  
  35. @Test
  36. public void testFileUpload() throws Exception{
  37. final File file = getFileFromResource(fileName);
  38. //File is correctly loaded
  39. final MockMultipartFile multipartFile = new MockMultipartFile("aMultiPartFile.txt", new FileInputStream(file));
  40.  
  41. doNothing().when(aService).doSomethingOnDBWith(any(MultipartFile.class), any(String.class));
  42.  
  43. mockMvc.perform(
  44. post("/file-upload")
  45. .requestAttr("file", multipartFile.getBytes())
  46. .requestAttr("something", ":(")
  47. .contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
  48. .andExpect(status().isCreated());
  49. }
  50. }
  51.  
  52. java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement