我正在尝试为 Play 2 控制器创建一个功能测试,该控制器将多部分表单数据作为输入。FakeRequest 目前没有方法支持多部分表单 POST。还有什么其他方法可以测试这个控制器?
Map<String, Object> map = new HashMap<String, Object>();
map.put("param1", "test-1");
map.put("param2", "test-2");
map.put("file", file)
Result result = routeAndCall(fakeRequest(POST, "/register").withFormUrlEncodedBody(map));// NO SUCH METHOD
编辑:这是我为测试多部分所做的解决方法。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:3333/blobupload");
FileBody imageFile = new FileBody(new File("test/resources/test-1.jpg"));
StringBody guid1 = null;
StringBody guid2 = null;
try {
guid1 = new StringBody("GUID-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("key1", imageFile);
reqEntity.addPart("key2", guid1);
httppost.setEntity(reqEntity);
HttpResponse response;
try {
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}