相关疑难解决方法(0)

在Play 2.0 FakeRequest中测试MultipartFormData

我正在尝试为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
Run Code Online (Sandbox Code Playgroud)

编辑:这是我测试多部分的解决方法.

    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 …
Run Code Online (Sandbox Code Playgroud)

java playframework-2.0

9
推荐指数
1
解决办法
8735
查看次数

标签 统计

java ×1

playframework-2.0 ×1