7

ここで提案されているように、Scala API を使用してこれを行うことができることを理解しています。

https://groups.google.com/forum/?fromgroups=#!topic/play-framework/1vNGW-lPi9I

しかし、FakeRequests の withFormUrlEncodedBody メソッドでは文字列値のみがサポートされているため、Java を使用してこれを行う方法はないようです。

これは API に欠けている機能ですか、それとも回避策はありますか? (Java のみを使用)。

4

1 に答える 1

2

統合テストでは、私のように apache DefaultHttpCLient を使用できます。

@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);
}

これには、テストでサーバーを起動する必要があります。

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);
}
于 2013-02-27T12:36:11.080 に答える