【问题标题】:Test POST method to send the file content测试POST方法发送文件内容
【发布时间】:2011-06-23 02:14:44
【问题描述】:
我在 Controller 中有一个方法,它将 MultipartFile(Spring class ) 对象作为并存储它。我必须做一个集成测试。
因此,当用户登录时,它应该能够上传文件。我正在使用 htmlUnit Webresponse 类将请求发送到控制器@localhost。
如何将 Multipartfile 对象添加到 Webresponse 或有其他方法来测试它?
谢谢
粗制滥造
【问题讨论】:
标签:
java
spring
integration-testing
htmlunit
【解决方案1】:
我找到了解决办法
MultipartPostMethod method =
new MultipartPostMethod( "http://localhost:8080/sendfile" );
method.addParameter("mediaFile", multifile );
HttpClient client1 = new HttpClient( );
// Execute and print response
client1.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );
return response;
该类是 apache-httpclient jar 的一部分