0

I am trying to test file upload using spring test.

@Test
    public void testUploadDocument() {

File f = new File("C:\\Users\\JC\\Documents\\sample_test_2.xlsx");
        System.out.println(f.isFile()+"  "+f.getName()+f.exists());
        FileInputStream fi1 = new FileInputStream(f);
        MockMultipartFile fstmp = new MockMultipartFile("file", f.getName(), "text/plain", fi1);
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        mockMvc.perform(MockMvcRequestBuilders.fileUpload("http://" + hostPort + "/AdminService/rest/document/saveDocument")
                .file(fstmp)
                .param("siteId","555").param("docTypeId","550").param("docSubTypeId", "5051").param("contentDate", "06-Oct-2017").param("contentDescription", "")
                .param("docName", "Test Co-op Unit 2").param("isNewFile", "true"))              
                .andExpect(MockMvcResultMatchers.status().isOk());
}

It is throwing Requested Method POST not supported even though I declared it as POST. Also I tried MockMvcRequestBuilders.post. But same problem.

 @RequestMapping(value = "/saveDocument", method = RequestMethod.POST)
        public ResponseEntity<String> saveDocument(
                @RequestParam("siteId") Integer siteId, @RequestParam("docTypeId") Integer docTypeId,
                @RequestParam("docSubTypeId") Integer docSubTypeId, @RequestParam("docName") String docName,
                @RequestParam(value = "contentDescription", required = false) String contentDescription,
                @RequestParam(value = "contentDate", required = false) String contentDate, @RequestParam(value = "docId", required = false) Integer docId,
            @RequestParam(value = "isNewFile", required = false) boolean isNewFile,
            @RequestParam(value = "file", required = false) MultipartFile file)
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Have you tried this answer : https://stackoverflow.com/a/21801067/369946 – Matt Oct 06 '17 at 15:04
  • Moved from an answer below - would you reply on Bastian's answer? _I need a bit more debugging information. Can you please provide more details about the error message? Also a bit more information about the controller and your general configuration regarding spring would be nice._ – halfer Oct 06 '17 at 21:26
  • @Matt i have tried the answer.. same problem. – Mallikarjun Challa Oct 10 '17 at 09:57

0 Answers0