title: How to write  unit test for CommonsMultipartFile with Mock in Spring
body: <p>I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit test case for file upload using Mockito.</p>
  <p>Following is my controller class</p>
  <pre><code>private RegistrationService registrationService;
  
  @RequestMapping(method = RequestMethod.POST)
      public String create(Registration registration, BindingResult result,ModelMap model)
              throws NumberFormatException, Exception {
          
          File uploadedFile = uploadFile(registration);
          List&lt;Registration&gt; userDetails = new ArrayList&lt;Registration&gt;();
          processUploadedFile(uploadedFile,userDetails);
          
          model.addAttribute(&quot;userDetails&quot;, userDetails);
          
          return &quot;Registration&quot;;
      }
      
      private File uploadFile(Registration registration) {
          
          Date dt = new Date();
          SimpleDateFormat format = new SimpleDateFormat(&quot;MM_dd_yyyy_HH_mm_ss&quot;);
          File uploadedFile = new File(uploadFileLocation
                  + registration.getFileData().getOriginalFilename() + &quot;.&quot;
                  + format.format(dt));
           
          try {
              registration.getFileData().transferTo(uploadedFile);
              
          } catch (IllegalStateException e) {
              logger.error(&quot;Error occurred while uploading file&quot;, e);  
          } catch (IOException e) {
              logger.error(&quot;Error occurred while uploading file&quot;, e);
          }   
          
          return uploadedFile;
      }
  
      private void processUploadedFile(File uploadedFile, List&lt;Registration&gt; userDetails)
              throws NumberFormatException, Exception {
  
          registrationService.processFile(uploadedFile, userDetails);
      }
  </code></pre>
  <p>Registration class</p>
  <pre><code>public class Registration {
      private String name;
      private CommonsMultipartFile fileData;
      
      
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public CommonsMultipartFile getFileData() {
          return fileData;
      }
  
      public void setFileData(CommonsMultipartFile fileData) {
          this.fileData = fileData;
      }
  }
  </code></pre>
  <p>My Service class method</p>
  <pre><code>public void processFile(File uploadedFile,
              List&lt;Registration&gt; userDetails) throws NumberFormatException,
              Exception {     
  
          String record = &quot;&quot;;
  
          try {
  
              FileWriter fstream = new FileWriter(outputFileLocation
                      + uploadedFile.getName());
              BufferedWriter out = new BufferedWriter(fstream);
  
              BufferedReader br = new BufferedReader(new FileReader(
                      uploadedFile.getAbsolutePath()));
  
              while ((record = br.readLine()) != null) {
                  String[] requesterUser = record.split(&quot;,&quot;);
  
                  if (insertUserToDB(requesterUser, userDetails) &gt; 0)
                      out.write(record + &quot;, Yes&quot;);
                  else
                      out.write(record + &quot;, No&quot;);
  
                  out.newLine();
  
              }
              out.flush();
              out.close();
  
          } catch (FileNotFoundException e) {
              logger.error(&quot;Error occured while processing file&quot;,  e);
          } catch (IOException e) {
              
              logger.error(&quot;Error occured while processing file&quot;,  e);
          }
  
          
      }
  </code></pre>
  <p>How to Unit Test it?I am new to Mockito.</p>
  <p><strong>Edit</strong></p>
  <p>I have write down Test method for testing above code, but it return null for multipartFile. How should I test above service class processFile method?</p>
  <pre><code>@Test
         public void testProcessFile() {
          private static final String TEST_FILE = &quot;c:\\user.csv&quot;;
          MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
          MockMultipartHttpServletRequest mockMultipartHttpServletRequest = (MockMultipartHttpServletRequest)request;
          MultipartFile multipartFile = (MultipartFile) mockMultipartHttpServletRequest.getFile(TEST_FILE);
  
          Registration registration=new Registration();
          registration.setFileData(multipartFile);
          RegistrationService.processFile(uploadedFile, userDetails);
          }
  </code></pre>
  <p>I am new to JUnit and mockito. Any help or pointer really appreciated.</p>