title: Writing JUnit Test for FileUpload Spring MVC Controller
body: <p>I have a Spring controller that is performing a FileUpload. My controller is taking a HTTP request for a multipart file using @RequestParam. My problem is I am not sure how I can write a JUnit test for my controller. I want to pass in a file stored in my src/main/resources and make sure it processes and writes out the content. Here is my controller</p>
  
  <pre><code>@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("&gt;&gt;&gt; uploadFileHandler started");
  
          // Check if file is multipart file
          if (file.getContentType() != null) {
              try {
  
                  Date uploadDate = new Date();
                  //LOGGER.info("&gt;&gt;&gt; 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&lt;Row&gt; ite = sheet.rowIterator();
                  //LOGGER.info("&gt;&gt;&gt; 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&lt;Cell&gt; 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("&gt;&gt;&gt; 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!";
      }
  
  }
  </code></pre>
  
  <p>How would I go about writing a test case? Is it possible to do this without using Mock? Could I expose it as a component and have it either accept an fileinputstream or file from request param?</p>