1

このhttpunitテストケースがwc.getResponseで「不正なファイル記述子」で失敗し続ける理由を誰かが説明できますか? 推測として is.close() を追加し、失敗の前後に移動しましたが、効果はありませんでした。このテストでは、リクエストを Dropwizard アプリに送信します。

    public class TestCircuitRequests
{
    static WebConversation wc = new WebConversation();
    static String url = "http://localhost:8888/funl/circuit/test.circuit1";

@Test
public void testPut() throws Exception
{
    InputStream is = new FileInputStream("src/test/resources/TestCircuit.json");
    WebRequest rq = new PutMethodWebRequest(url, is, "application/json");

    wc.setAuthentication("FUNL", "foo", "bar");
    WebResponse response = wc.getResponse(rq);
    is.close();
}
4

1 に答える 1

0

無反応?だから、これと戦って学んだことに基づいて、自分自身を試してみます.

Httpunit は、できれば使用したい使い慣れたツールです。しかし、それは 2 年以上更新されていないため、@PUT 要求のサポートは正しくありません。

そこで、代わりに Jersey-client に変換しました。苦労した後、うまくいくように見えるこのコードにたどり着きました:

    @Test
public void testPut() throws Exception
{
    InputStream is = new FileInputStream("src/test/resources/TestCircuit.json");
    String circuit = StreamUtil.readFully(is);
    is.close();

    Authenticator.setDefault(new MyAuthenticator());
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    com.sun.jersey.api.client.WebResource service = client.resource(url);

    Builder builder = service.accept(MediaType.APPLICATION_JSON);
    builder.entity(circuit, MediaType.APPLICATION_JSON);
    builder.put(String.class, circuit);
    return;
}

これにより、JSON 文字列からの Bean の JAX-RS 自動構築が意図的に回避されます。

于 2012-10-05T23:40:32.140 に答える