2016-09-15 55 views
0

我有一个执行FileUpload的Spring控制器。我的控制器正在使用@RequestParam对多部分文件进行HTTP请求。我的问题是我不知道如何为我的控制器编写JUnit测试。我想传入存储在我的src/main/resources中的文件,并确保它处理并写出内容。这里是我的控制器编写FileUpload的JUnit测试Spring MVC控制器

@RequestMapping(value = "/DefectImport", method = RequestMethod.POST) 
public @ResponseBody 

// Request file from upload explorer as a multipart file 
String uploadFileHandler(@RequestParam("file") MultipartFile file){ 
    //LOGGER.info(">>> uploadFileHandler started"); 

     // Check if file is multipart file 
     if (file.getContentType() != null) { 
      try { 

       Date uploadDate = new Date(); 
       //LOGGER.info(">>> Date: " + uploadDate); 
       System.out.println(uploadDate); 

       //Get input stream of the file 
       InputStream is = file.getInputStream(); 

       // Finds the workbook instance for XLSX file 
       XSSFWorkbook workbook = new XSSFWorkbook (is); 

       // Return first sheet from the XLSX workbook 
       XSSFSheet sheet = workbook.getSheetAt(0); 

       // Get iterator to all the rows in current sheet 
       Iterator<Row> ite = sheet.rowIterator(); 
       //LOGGER.info(">>> Writing Started of file " + file.getOriginalFilename()); 
       System.out.println("Writing Started of file " + file.getOriginalFilename()); 

       // Traversing over each row of XLSX file 
       while(ite.hasNext()){ 
        Row row = ite.next(); 

        // For each row, iterate through each column 
        Iterator<Cell> cite = row.cellIterator(); 
        while(cite.hasNext()){ 
         Cell c2 = cite.next(); 

         // Check for different data types and return value 
         switch (c2.getCellType()) { 
         case Cell.CELL_TYPE_STRING: 
          //LOGGER.info(c2.getStringCellValue() + " "); 
          System.out.print(c2.getStringCellValue() + " "); 
          break; 
         case Cell.CELL_TYPE_NUMERIC: 
          if (DateUtil.isCellDateFormatted(c2)) 
          { 
           SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
           //LOGGER.info(dateFormat.format(c2.getDateCellValue()) + " "); 
           System.out.print(dateFormat.format(c2.getDateCellValue()) + " "); 
          } 
          else 
          { 
           //LOGGER.info(c2.getNumericCellValue() + " "); 
           System.out.print(c2.getNumericCellValue() + " "); 
          }        
          break; 
         case Cell.CELL_TYPE_BOOLEAN: 
          //LOGGER.info(c2.getBooleanCellValue() + " "); 
          System.out.print(c2.getBooleanCellValue() + " "); 
          break; 
         default:        
         } 
        } 
        //LOGGER.debug(); 
        System.out.println(); 
       } 
       is.close(); 
       workbook.close(); 
       //LOGGER.info(">>> uploadFileHandler complete"); 
       System.out.println("Writing finished..."); 

      } 

      /** 
      * Error handling 
      */ 
      /*catch (InvalidFormatException e) 
      { 

      }*/ 
      catch (MaxUploadSizeExceededException e) 
      { 
       return "The file you uploaded is too large"; 
      } 
      catch (FileNotFoundException fe) 
      { 
       System.out.println("File not found"); 

      } 
      catch (IOException ie) 
      { 
       System.out.println("The file you uploaded is not an XLSX file"); 
      }  
     }   

     return "Thank you for your submission!"; 
    } 

} 

我该如何去写一个测试用例?是否有可能做到这一点,而不使用模拟?我可以公开它作为一个组件,并让它接受来自请求参数的文件输入流或文件吗?

回答

0

您不需要测试文件的上传。您可能想要测试的一点是控制器接收文件并正确处理文件。为此,我建议从资源目录中嘲笑和提供服务。

在编写测试时,总是试着按照给定的时间,然后模型。例如:

@Test 
public void uploadFileTest() throws Exception{ 
    //given 
    InputStream uploadStream = UploadControllerTest.class.getClassLoader().getResourceAsStream("exceldocument.xlsx"); 
    MockMultipartFile file = new MockMultipartFile("file", uploadStream); 
    assert uploadStream != null; 

    //when 
    this.mockMvc.perform(fileUpload("/DefectImport") 
      .file(file)) 
    //then 
      .andExpect(status().isOk()); 
} 

这会嘲笑文件的分段上传,并检查InputStream是否为空,且上传状态是否正常(200)。