如何在没有框架的情况下模拟 HttpServletRequest 中的文件有效负载?
我编写了一个 servlet,通过 HttpServletRequest
我需要测试它。不幸的是,工作中的框架不允许模拟框架,因此我需要为 JUnit 测试编写自己的 HttpServletRequest
模拟。
我正在使用一些可用的 Apache Commons 类来从请求中获取文件:
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
private static List<FileItem> getFilesFromReq(final HttpServletRequest request) {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> fileItems = new ArrayList<>();
try {
fileItems = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
return fileItems;
}
我编写了一个 HttpServletRequest 的实现作为模拟(实际上是公司中其他人编写的部分模拟的扩展) )。但我不知道如何添加到请求正文,更不用说如何将文件包含为 multipart/form-data
了。而且 HttpServletRequest 中似乎没有任何方法甚至引用请求正文。
我在网上找到了 DiskFileItemFactory
/FileItemFactory
和 ServletFileUpload
,但它的工作原理以及文件如何包含在请求中对我来说仍然相当不透明。
谁能给我一些在模拟 HttpServletRequest
中将文件添加到 multipart/form-data
的指导?
I've written a servlet to upload files to the server via HttpServletRequest
and I need to test it. Unfortunately the framework at work isn't allowing mocking frameworks, so I need to write my own mock of HttpServletRequest
for JUnit tests.
I'm using some Apache Commons classes we have available to get files from the request:
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
private static List<FileItem> getFilesFromReq(final HttpServletRequest request) {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> fileItems = new ArrayList<>();
try {
fileItems = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
return fileItems;
}
I've written an implementation of HttpServletRequest
as the mock (actually an extension of a partial mock someone else in the company wrote). But I can't figure out how to add to the request body, let alone how to include files as multipart/form-data
. And there don't seem to be any methods in HttpServletRequest
even referencing the request body.
I found online the source code of DiskFileItemFactory
/FileItemFactory
and ServletFileUpload
, but how it works and how the files are included in the request is still rather opaque to me.
Can anyone give me some guidance for adding files to multipart/form-data
in a mock HttpServletRequest
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议首先将此方法设为非静态。接下来,不应在方法中实例化 ServletFileUpload 实例,而是应将其注入类的构造函数中。
现在,在您的测试中,您可以传入 ServletFileUpload 的模拟。您可以使用 Mockito 或 JMock 或创建自己的模拟类。执行此操作后,您将能够验证模拟类的方法 parseRequest 是否被调用。
I would recommend making this method non-static first of all. Next, instead of instantiating the ServletFileUpload instance in the method it should be injected in the constructor of your class.
Now in your test you can pass in a mock of ServletFileUpload. You can use Mockito or JMock or create your own mock class. After doing this you'll be able to verify that the your mocked class's method parseRequest was called.