【问题标题】:Loopj (AsyncHttpClient get method) doesnt return response in Android Unit TestLoopj(AsyncHttpClient get 方法)在 Android 单元测试中不返回响应
【发布时间】:2013-12-04 19:36:57
【问题描述】:

我正在尝试在处理 URL 请求的 Android 项目上创建单元测试。我使用 loopj 库,但有些东西不起作用。我的清单中启用了 Internet:

<uses-permission android:name="android.permission.INTERNET" />

测试方法中的Java代码:

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://www.yahoo.com", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println(response); // <------ I never get here!?!?!
        }
    });

以下过程(不带loopj)在相同的单元测试方法中工作:

    URL yahoo;
    yahoo = new URL("http://www.yahoo.com/");
    BufferedReader in;
    in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
        String inputLine;
    while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);

            }
    in.close();

似乎 loopj 请求在单元测试类中不起作用,但在基本 Activity 类中正常工作。有什么建议吗?

【问题讨论】:

    标签: android loopj


    【解决方案1】:

    问题是因为 loopj 使用 android.os.AsyncTask 在单元测试环境中不起作用。

    成功的关键是“runTestOnUiThread”方法。

    public void testAsyncHttpClient() throws Throwable {
      final CountDownLatch signal = new CountDownLatch(1);
      final AsyncHttpClient httpClient = new AsyncHttpClient();
      final StringBuilder strBuilder = new StringBuilder();
    
      runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
        @Override
        public void run() {
          httpClient
              .get(
                  "https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",
                  new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(String response) {
                      // Do not do assertions here or it will stop the whole testing upon failure
                      strBuilder.append(response);
                    }
    
                    public void onFinish() {
                      signal.countDown();
                    }
                  });
        }
      });
    
      try {
        signal.await(30, TimeUnit.SECONDS); // wait for callback
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    
      JSONObject jsonRes = new JSONObject(strBuilder.toString());
      try {
        // Test your jsonResult here
        assertEquals(6253282, jsonRes.getInt("id"));
      } catch (Exception e) {
    
      }
    
      assertEquals(0, signal.getCount());
    }
    

    全线程: https://github.com/loopj/android-async-http/issues/173

    【讨论】:

      【解决方案2】:

      注意你没有忘记在清单中声明网​​络访问权限,即

      <manifest ....>
      ...
          <uses-permission android:name="android.permission.INTERNET" />
      ...
      </manifest>
      

      【讨论】:

        【解决方案3】:

        一个基于异步回调的 Android Http 客户端,构建在 Apache 的 HttpClient 库之上。所有请求都在您应用的主 UI 线程之外发出,但任何回调逻辑都将在使用 Android 的 Handler 消息传递创建回调的同一线程上执行。

        检查http://loopj.com/android-async-http/这可能对你有帮助!

        【讨论】:

        • 它在正常的 Android Activity 类中按预期工作,但我试图在不返回任何响应的 Android 单元测试环境中使用这些方法..
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多