0

I want to test the following method, and verify

fi.write( file ) ;

gets executed.

@RequestMapping(value="UploadServlet", method=RequestMethod.POST)
public String uploadfile(HttpServletRequest request, HttpServletResponse response, HttpSession session ) throws IOException{
    filePath = "C:\\JavaWorkspace\\justbuyit\\src\\main\\webapp\\resources\\images\\";
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(largefile);

      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(request);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fileName = fi.getName();   
            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( filePath + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            session.setAttribute("filepath","resources/images/"+fileName.substring(fileName.lastIndexOf("\\")+1) );
            fi.write( file ) ;
            request.setAttribute("filewritten", true);
            request.setAttribute("filename", fileName.substring(fileName.lastIndexOf("\\")+1));
         }
      }
   }catch(Exception ex) {
       System.out.println(ex);
   }

    return "addProduct";
   }

public void setFactory(DiskFileItemFactory factory) {
    this.factory = factory;
}

public void setUpload(ServletFileUpload upload) {
    this.upload = upload;
}

I wrote the following test in mockito:

@Before

public void setUp(){

    MockitoAnnotations.initMocks(this);


    uController = new UploadController();
}

@Test

public void testWriteCommandGetsExecutes() throws Exception{


    uController.setFactory(factory);
    uController.setUpload(upload);
    when(i.hasNext()).thenReturn(false);
    when((FileItem)i.next()).thenReturn(fi);
    when(fi.isFormField()).thenReturn(false);
    uController.uploadfile(request, response, session);
    verify(fi).write(file);
}}

However I get error

wanted but not invoked: fi.write( file ) 

In my coverage this line shows up as yellow:

while ( i.hasNext () ) 

What is the problem?

hsp
  • 11
  • 3
  • It doesn't return true! you need return true and then false. – David Pérez Cabrera Jun 24 '15 at 17:34
  • you return i.hasNext as false, first it needs to be true to go inside the loop, and the error you are getting means mockito is expecting the fi.write call but the call is never been made,since the program doesnt even enter your loop – Gowrav Jun 24 '15 at 17:44

1 Answers1

1

As David Perez Cabrera and striker mentioned in the comments, it looks like you'll want your block to return true and then false. Mockito supports a couple of syntax choices for this, so either of these should work:

when(i.hasNext()).thenReturn(true, false);

or

when(i.hasNext()).thenReturn(true).thenReturn(false);

For future reference, Mockito's behavior is to do the actions in order and then forever repeat the last one, which means that calls to hasNext stubbed above will return true, false, false, false and so on as opposed to true, false, true, false, true and so on.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • I made following changes but still the error persists – hsp Jun 25 '15 at 10:40
  • Where do you get the `file` in your test? Beware that `verify` will test using `File.equals`, which [may not behave in an intuitive way](http://stackoverflow.com/q/8930859/1426891). – Jeff Bowman Jun 25 '15 at 17:07