Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何准备具有完整头信息的HttpSerlvetRequest对象_Java_Spring_Junit - Fatal编程技术网

Java 如何准备具有完整头信息的HttpSerlvetRequest对象

Java 如何准备具有完整头信息的HttpSerlvetRequest对象,java,spring,junit,Java,Spring,Junit,如何为Junit测试生成HttpServletRequest对象。使用完整的标题信息,如内容类型、状态代码、方法、mime类型、语言等。如果您使用的是Spring,则可以使用该类: org.springframework.mock.web.MockHttpServletRequest 下面是发布json对象的http请求的示例: final MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.POST.na

如何为Junit测试生成HttpServletRequest对象。使用完整的标题信息,如内容类型、状态代码、方法、mime类型、语言等。

如果您使用的是Spring,则可以使用该类:
org.springframework.mock.web.MockHttpServletRequest

下面是发布json对象的http请求的示例:

final MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.POST.name(), "path for request");
request.addHeader("content-type", "application/json");
request.setContentType("application/json");
request.setContent(this.gson.toJson(object).getBytes("UTF-8"));

假设我使用的内容类型是multiparty/form data,那么如何在请求对象中添加信息,谢谢。您可以使用
Apache Commons HttpClient
MultipartRequestEntity
类。见下文