如何使用commons-fileupload编写单元测试?

时间:2010-12-17 00:41:21

标签: java testing mockito apache-commons-fileupload

我正在使用commons-fileupload的框架中工作。

现在我需要编写单元测试类。如果ServletFileUpload不是接口,我的疑问是如何编写模拟测试?还有另一种测试我的课程的方法吗?

昨天我使用mockito为Servlet Parts编写单元测试,这很简单。但我想不出如何为commons-fileupload编写测试。

3 个答案:

答案 0 :(得分:1)

您可以使用Commons HTTPClient构建多部分流:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletFileUploadMock {

    public static class FileUpload {

        private final String filename;
        private final String mimeType;
        private final byte[] contents;

        public FileUpload(String filename, String mimeType, byte[] contents) {
            this.filename = filename;
            this.mimeType = mimeType;
            this.contents = contents;
        }

    }

    public static HttpServletResponse mockServletFileUpload(HttpServlet servlet, List<FileUpload> files,
            Map<String, String> queryParams) throws IOException, ServletException {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        for (FileUpload f : files) {
            builder = builder.addBinaryBody(f.filename, f.contents, ContentType.create(f.mimeType), f.filename);
        }
        HttpEntity entity = builder.build();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        entity.writeTo(os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());

        HttpServletRequest mockRequest = mock(HttpServletRequest.class);
        when(mockRequest.getMethod()).thenReturn("POST");
        when(mockRequest.getContentType()).thenReturn(entity.getContentType().getValue());
        when(mockRequest.getContentLength()).thenReturn((int)entity.getContentLength());
        when(mockRequest.getInputStream()).thenReturn(new MockServletInputStream(is));

        // Mock query params
        for (Entry<String, String> p : queryParams.entrySet()) {
            when(mockRequest.getParameter(p.getKey())).thenReturn(p.getValue());
        }

        HttpServletResponse mockResponse = mock(HttpServletResponse.class);

        servlet.service(mockRequest, mockResponse);

        return mockResponse;
    }

    public static class MockServletInputStream extends ServletInputStream {

        private final InputStream delegate;

        public MockServletInputStream(InputStream delegate) {
            this.delegate = delegate;
        }

        @Override
        public int read() throws IOException {
            return delegate.read();
        }

    }
}

答案 1 :(得分:0)

您可以回到基础并通过包装或扩展和覆盖来手动滚动模拟对象。有时它更容易不依赖于模拟框架的一切。

Mocking Mocking and Testing Outcomes

答案 2 :(得分:0)

我会考虑将FileUpload调用包装在另一层中。虽然它可能听起来有点过分,但它可以让你快速切换上传库,增加了更好的单元测试你的呼叫的能力......看起来你正在紧密地将你的应用程序耦合到commons-fileupload ......