1
我已經嘗試運行以下JUnit測試。當我嘗試打印出userHash字符串時,我得到該字符串爲空。這表明httpclient根本沒有運行。我一直在關注這個帖子...使用JUNIT測試運行AsyncHttpClient
https://github.com/loopj/android-async-http/issues/173
這裏是我的課。
public class ECApiManagerTests {
InstrumentationTestCase runnerHelper = new InstrumentationTestCase();
private final String userEmail = "removed";
private final String passWord = "removed";
String userHash;
@Test
public void testPOST()throws Throwable{
final AsyncHttpClient httpClient = new AsyncHttpClient();
runnerHelper.runTestOnUiThread(new Runnable() {
@Override
public void run() {
RequestParams params = new RequestParams();
params.put("email", userEmail);
params.put("password", passWord);
httpClient.post("https://edu.chat/api/login/", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//called when response code 200
userHash = new String(responseBody);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
});
try {
System.out.println(userHash);
JSONObject obj = new JSONObject(userHash);
Assert.assertEquals("true", obj.getString("success"));
} catch (JSONException e) {
e.printStackTrace();
}
}
感謝這一點,但由於某種原因字符串仍爲空... –
您是否添加日誌記錄以查看您的'onFailure'是否被調用。如果是這樣,'userHash'將會是'null'。 – iagreen
是的。它似乎從來沒有被稱爲..你知道這是爲什麼嗎? –