hsp
hsp

Reputation: 11

Wanted but not invoked mockito test

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?

Upvotes: 0

Views: 370

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95634

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.

Upvotes: 1

Related Questions