使用MockMultipartHttpServletRequest进行单元测试(在ItemInputStream.makeAvailable中抛出NullPointerException)

时间:2012-02-28 00:54:44

标签: java spring unit-testing junit jmock

我编写了一个转换器类,它接受HttpServletRequest并将其转换为另一种类型,该类型保存来自servlet请求的InputStream指针。 (这个想法是从请求处理中抽象出传输传输协议,所以我也可以从FTP编写类似的转换器。)

现在我正在尝试为此编写单元测试,但我遇到了问题。我已经设法找出正确的样板来创建有效的Multipart HTTP请求(使用Spring类MockMultipartHttpServletRequest和MockMultipartFile),但现在我在initialize()类的UploadRequest方法中得到了NullPointerException。我猜测问题是,MockMultipartHttpServletRequest中的流不能正确初始化,但我无法弄清楚我应该做些什么不同。

任何建议都会被感激地接受!

这是堆栈跟踪:

java.lang.NullPointerException
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:976)
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:886)
    at java.io.InputStream.read(InputStream.java:82)
    at org.apache.commons.fileupload.util.Streams.copy(Streams.java:96)
    at org.apache.commons.fileupload.util.Streams.copy(Streams.java:66)
    at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:592)
    at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:618)
    at org.apache.commons.fileupload.MultipartStream.skipPreamble(MultipartStream.java:637)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:984)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
    at com.ooyala.UploadRequest.initialize(UploadRequest.java:51)
    at com.ooyala.UploadRequestTest.testCreateFromServletRequest(UploadRequestTest.java:57)

这是我的变压器类的缩写版本:

public class UploadRequest {
  private Map<String, String> params;
  private InputStream strIn;
  private Logger Log = Logger.getLogger(UploadRequest.class.getName());

  public UploadRequest()
  {
    params = new HashMap<String, String>();
  }

  public void initialize(HttpServletRequest sRequest, 
                         ServletFileUpload upload)
    throws IOException, FileUploadException
  {
    Enumeration<String> paramNames = sRequest.getParameterNames();
    while (paramNames.hasMoreElements()) {
      String pName = paramNames.nextElement();
      params.put(pName, sRequest.getParameter(pName));
    }
    params.put("request_uri", sRequest.getRequestURI());

    FileItemIterator iter = upload.getItemIterator(sRequest);
    while (iter.hasNext()) {
      FileItemStream item = iter.next();
      try {
        if (!item.isFormField()) {
          // Skip form fields
          params.put("original_file_name", item.getName());
          strIn = item.openStream();
        } 
      } catch (IOException ex) {
        Log.severe("File uploading exception: " + ex.getMessage());
        throw ex;
      }
    }
  }

这是单元测试:

import org.springframework.mock.web.MockMultipartHttpServletRequest;
import org.springframework.mock.web.MockMultipartFile;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
// etc.... other imports

@RunWith(JMock.class)
public class UploadRequestTest {
  private UploadRequest upRequest;

  @Before
    public void setUp()
    {
      context.setImposteriser(ClassImposteriser.INSTANCE);
      upRequest = new UploadRequest();
    }

  @Test
    public void testCreateFromServletRequest()
      throws IOException, FileUploadException
    {
      String text_contents = "hello world";

      MockMultipartHttpServletRequest sRequest = 
        new MockMultipartHttpServletRequest();
      sRequest.setMethod("POST");
      String boundary = generateBoundary();
      String contentType = "multipart/form-data; boundary="+boundary;
      sRequest.setContentType(contentType);
      sRequest.setRequestURI("/foo");
      sRequest.addParameter("test_param","test_value");
      sRequest.addFile(
        new MockMultipartFile("file1","test_upload.txt","text/plain",
          text_contents.getBytes()));

      ServletFileUpload upload = new ServletFileUpload();
      assertTrue(upload.isMultipartContent(sRequest));

      upRequest.initialize(sRequest, upload);
    }
}

3 个答案:

答案 0 :(得分:1)

我有同样的问题,我用谷歌搜索但没有回答。我插入了库中的源代码,你需要发送内容,无论如何。库可能需要检查skip方法中是否为null

MockMultipartHttpServletRequest request
request.setContent("whatever".getBytes());

在这里发布给其他人

答案 1 :(得分:1)

  1. 添加边界条件
  2. 生成内容如下

    MockMultipartHttpServletRequest request = 
        this.generateMockMultiPartHttpServletRequest(true);
    MockMultipartFile mockMultipartFile = null;
    try {
        request.setContentType("multipart/form-data; boundary=-----1234");
        request.setCharacterEncoding("text/plain");
        String endline = "\r\n";
        String bondary = "-----1234";
        String textFile = this.encodeTextFile("-----1234", "\r\n", "file","test.csv",
            "text/UTF-8", FileUtils.readFileToString((new File(csvFilePath)), "UTF-8"));
        StringBuilder content = new StringBuilder(textFile.toString());
        content.append(endline);
        content.append(endline);
        content.append(endline);
        content.append("--");
        content.append(bondary);
        content.append("--");
        content.append(endline);
        request.setContent(content.toString().getBytes());
        request.setMethod("POST");
        mockMultipartFile = new MockMultipartFile("file",
        FileUtils.readFileToByteArray(new File(csvFilePath)));
    } catch (Exception e1) {
        e1.printStackTrace();
    }
     request.addFile(mockMultipartFile);
    
  3. 编码文本的功能

        private String encodeTextFile(String bondary, String endline, String name, 
            String filename, String contentType, String content) {
    
            final StringBuilder sb = new StringBuilder(64);
            sb.append(endline);
            sb.append("--");
            sb.append(bondary);
            sb.append(endline);
            sb.append("Content-Disposition: form-data; name=\"");
            sb.append(name);
            sb.append("\"; filename=\"");
            sb.append(filename);
            sb.append("\"");
            sb.append(endline);
            sb.append("Content-Type: ");
            sb.append(contentType);
            sb.append(endline);
            sb.append(endline);
            sb.append(content);
            return sb.toString();
        }
    

答案 2 :(得分:1)

我经历了同样的问题,经过搜索很多我得到了这篇帖子,其中我回答了解决我问题的代码。

Shriprasad的解决方案适用于文本文件。但我在二进制文件方面遇到了一些问题。

https://stackoverflow.com/a/30541653/2762092