Sto provando a creare un test di funzionalità per un controller di Play 2 che accetta come input dati di modulo multiparte. Al momento non esiste alcun metodo in FakeRequest per supportare il POST di form multipart. Quali altri modi per testare questo controller?Test MultipartFormData in Play 2.0 FakeRequest
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
EDIT: Questa è la soluzione che ho fatto per testare multipart.
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();
}
Perché callAction è necessario per withFormUrlEncodeBody? routeAndCall dovrebbe funzionare anche. routeAndCall (fakeRequest (POST, "/route").withFormUrlEncodedBody(map)); – angelokh
Mutlipartformdata potrebbe funzionare per il metodo scala con te. Tuttavia, per java, withFormUrlEncodedBody non consentirebbe una mappa con valore nel tipo di oggetto. – angelokh
Non so perché callAction è richiesto su Java flavor ma sembra che tu possa solo chiamare fakeRequest(). ConFormUrlEncodeBody con callAction, comunque, non ne so molto del linguaggio Java, quindi posso sbagliarmi completamente. Non penso che tu debba usare con FormUrlEncodedBody per testare il tuo multipart, dovresti solo costruire la richiesta POST corretta. – DamnWidget