读书人

怎么在Android单元测试中调试async-ht

发布时间: 2013-10-12 11:54:04 作者: rapoo

如何在Android单元测试中调试async-http
转自:https://github.com/loopj/android-async-http/issues/173

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

读书人网 >Android

热点推荐