Reputation: 955
I understand that you can do this using the Scala API as suggested here:
https://groups.google.com/forum/?fromgroups=#!topic/play-framework/1vNGW-lPi9I
But there seems to be no way of doing this using Java as only string values are supported in FakeRequests' withFormUrlEncodedBody method?
Is this a missing feature in the API or is there any workaround? (Using only Java).
Upvotes: 7
Views: 2886
Reputation: 397
For integration testing you can use apache DefaultHttpCLient like I do:
@Test
public void addFileItem() throws Exception {
File testFile = File.createTempFile("test","xml");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(URL_HOST + "/api/v1/items/file");
MultipartEntity entity = new MultipartEntity();
entity.addPart("description", new StringBody("This is my file",Charset.forName("UTF-8")));
entity.addPart(Constants.ITEMTYPE_KEY, new StringBody("FILE", Charset.forName("UTF-8")));
FileBody fileBody = new FileBody(testFile);
entity.addPart("file", fileBody);
method.setEntity(entity);
HttpResponse response = httpclient.execute(method);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(CREATED);
}
This requires that you start a server in your tests:
public static FakeApplication app;
public static TestServer testServer;
@BeforeClass
public static void startApp() throws IOException {
app = Helpers.fakeApplication();
testServer = Helpers.testServer(PORT, app);
Helpers.start(testServer);
}
@AfterClass
public static void stopApp() {
Helpers.stop(testServer);
}
Upvotes: 2