Announcement Announcement Module
Collapse
No announcement yet.
Problem testing upload servlet with Spring mocks Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem testing upload servlet with Spring mocks

    Hi,
    I'm trying to unit test an upload servlet using Spring mocks (MockMultipartFile / MockMultipartHttpServletRequest / MockHttpServletResponse) and I'm having some troubles.

    The servlet uses apache's commons-fileupload:

    Code:
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException
    {
        logger.info("Reached ImportServlet.doPost()");
    
        if (!ServletFileUpload.isMultipartContent(request)) {
          logger.error("Not a multipart request");
          response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Not multipart request");
          return;
        }
    
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    
        List<FileItem> fileItems;
        try {
          fileItems = upload.parseRequest(request);
        } catch (FileUploadException fue) {
          logger.error("FileUploadException while parsing request", fue);
          response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error parsing request");
          return;
        }
        ...
    }
    The unit test looks like:

    Code:
    public void testDoPost_MissingParam()
        throws IOException
    {
        String test = "dghdkhgidgdurg";
        MockMultipartFile file = new MockMultipartFile("import", "original-file-name.qif", "text/html", test.getBytes());
    
        mockMultipartRequest.setContentType("multipart/form-data");
        mockMultipartRequest.setMethod("POST");
        mockMultipartRequest.addFile(file);
    
        assertTrue(ServletFileUpload.isMultipartContent(mockMultipartRequest));
    
        importServlet.doPost(mockMultipartRequest, mockResponse);
    
        assert(HttpServletResponse.SC_BAD_REQUEST, mockResponse.getStatus());
    }
    The test expects the response status code to be SC_BAD_REQUEST because of a missing parameter in the request. (This is checked later in the doPost() method.)

    The problem is that the call to parseRequest() in doPost() fails with this exception:

    Code:
    2009-11-06 12:10:42,807 ERROR [main][ImportServlet]: FileUploadException while parsing request
    org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
    	at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:931)
    	at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    	at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
    	at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    	at com.ecomptes.gwt.server.imports.ImportServlet.doPost(ImportServlet.java:83)
    	at com.ecomptes.gwt.server.imports.ImportServletTest.testDoPost_MissingAccountId(ImportServletTest.java:93)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at junit.framework.TestCase.runTest(TestCase.java:168)
    	at junit.framework.TestCase.runBare(TestCase.java:134)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:124)
    	at junit.textui.TestRunner.doRun(TestRunner.java:116)
    	at com.intellij.rt.execution.junit.IdeaTestRunner.doRun(IdeaTestRunner.java:94)
    	at junit.textui.TestRunner.doRun(TestRunner.java:109)
    	at com.intellij.rt.execution.junit.IdeaTestRunner.startRunnerWithArgs(IdeaTestRunner.java:22)
    	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:118)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
    I've googled it for a little while but didn't find how to fix this problem
    An idea?

    Thks,
    Phil

  • #2
    Did anybody used MockMultipartHttpServletRequest successfully to unit test a servlet?
    I'm still stuck on this...

    Comment

    Working...
    X