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

发布于 2024-10-07 16:46:36 字数 199 浏览 4 评论 0原文

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

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

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

I'm working in a framework that uses commons-fileupload.

Now I need to write unit test classes. My doubt is how to write mock tests if ServletFileUpload is not an interface? There are another way to test my classes?

Yesterday I wrote unit tests using mockito for Servlet Parts, and it's easy. But I can't think how to write tests for commons-fileupload.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有你我更好 2024-10-14 16:46:36

您可以使用 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();
        }

    }
}

You could use Commons HTTPClient to build the multipart stream:

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();
        }

    }
}
伴随着你 2024-10-14 16:46:36

您可以回到基础知识并通过包装或扩展和覆盖来手动滚动您的模拟对象。有时,不依赖模拟框架来完成所有事情会更容易。

模拟模拟和测试结果

You could go back to basics and hand roll your mock object by either wrapping or extending and overriding. Sometimes its easier to not depend on a mocking framework for everything.

Mocking Mocking and Testing Outcomes

罪#恶を代价 2024-10-14 16:46:36

我会考虑将您的 FileUpload 调用包装在另一层中。虽然这听起来有点矫枉过正,但它可以让您非常快速地切换上传库,并具有更好地对您的调用进行单元测试的附加功能...看起来您将您的应用程序与 commons-fileupload 紧密耦合...

I would consider wrapping your FileUpload calls in another layer. While it might sound like overkill, it would allow you to switch upload libraries very quickly with the added ability to better unit test your calls...It seems like you are tightly coupling your app to commons-fileupload...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文