2 Replies Latest reply on May 12, 2008 7:51 PM by Tom Nichols

    Unit testing a multipart request

    Tom Nichols Newbie

      Hi -- I have a servlet that uses Seam's MultipartRequest.  However I can't really unit test it because MultipartRequest is not an interface (so I can't use EasyMock).  I also can't seem to subclass MultipartRequest either since its only public constructor seems to read the wrapped HttpServletRequest's POST body at the time of the constructor call.  I imagine this would apply to testing a JSF action bean as well (right?) 


      So it seems my only option for mocking this would be to create a mock HttpServletRequest that returns a properly formatted multi-part HTTP POST body, and then feed it to the MultipartRequest constructor... Which is a bit messy. 


      Ideally I'd simply do something like this:


      //somewhere in a unit test method...
      byte[] uploadData = // get test data from file
      
      MultipartRequest mockReq = new MulitpartRequest() {
        @Override public byte[] getFileBytes( String name ) {
          if ( name == "LOGFILE" ) return uploadData;
          throw new IllegalArgumentException( "unexpected file: " + name );
        }
      };
      
      myTestClass.handleRequest( mockReq );
      



      My consolation in the meantime has been to abstract away the request and response and test the code directly below that level, i.e. having a method that takes the file data and file name (and other relevant request params) as and returns a result.  Can anyone give advice on the proper way to unit test code which uses a MultipartRequest?  Or should I switch to Spring's multipart request handling which provides a proper MultipartHttpServletRequest interface?


      Thanks.