2016-09-15 18 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!"; 
    } 

} 

私はテストケースを書くにはどうしたらいいですか? Mockを使わずにこれを行うことは可能ですか?私はそれをコンポーネントとして公開して、それをfileinputstreamまたはrequest paramのファイルを受け入れることができますか?

答えて

0

ファイルのアップロードをテストする必要はありません。あなたがテストしたいと思われる点は、コントローラがファイルを受け取り、ファイルを正しく処理することです。このためには、リソースディレクトリからの嘲笑と提供をお勧めします。

テストを書くときは、常に、与えられた、モデル、モデルに従ってください。例えば:(200)

@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()); 
} 

これは、入力ストリームがnullでなく、ファイルをチェックのマルチパートアップロードをあざけり、アップロードのステータスがOKであること。

関連する問題