Java 使用JUNIT测试运行AsyncHttpClient
我一直在尝试运行以下JUnit测试。当我试图打印出userHash字符串时,我得到的结果是该字符串为null。这表明httpclient根本没有运行。我一直在关注这个帖子 这是我的班级Java 使用JUNIT测试运行AsyncHttpClient,java,android,Java,Android,我一直在尝试运行以下JUnit测试。当我试图打印出userHash字符串时,我得到的结果是该字符串为null。这表明httpclient根本没有运行。我一直在关注这个帖子 这是我的班级 public class ECApiManagerTests { InstrumentationTestCase runnerHelper = new InstrumentationTestCase(); private final String userEmail = "removed";
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();
}
}
您忘记了解决方案的
倒计时闩锁
部分。这一点很重要,因为请求是异步的,所以您需要等到请求完成后再查看结果。闩锁通过让调用线程等待响应处理程序调用onFinish
来实现这一点
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();
final CountDownLatch signal = new CountDownLatch(1);
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) {
}
@Override
public void onFinish() {
signal.countDown();
}
});
}
});
try {
signal.await(30, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
System.out.println(userHash);
JSONObject obj = new JSONObject(userHash);
Assert.assertEquals("true", obj.getString("success"));
} catch (JSONException e) {
e.printStackTrace();
}
}
您忘记了解决方案的
倒计时闩锁
部分。这一点很重要,因为请求是异步的,所以您需要等到请求完成后再查看结果。闩锁通过让调用线程等待响应处理程序调用onFinish
来实现这一点
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();
final CountDownLatch signal = new CountDownLatch(1);
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) {
}
@Override
public void onFinish() {
signal.countDown();
}
});
}
});
try {
signal.await(30, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
System.out.println(userHash);
JSONObject obj = new JSONObject(userHash);
Assert.assertEquals("true", obj.getString("success"));
} catch (JSONException e) {
e.printStackTrace();
}
}
您是否添加了日志记录以查看是否正在调用您的
onFailure
。如果是这样,userHash
将为null
Yes。好像从来没有人叫过它。。你知道这是为什么吗?你是否添加了日志以查看是否调用了你的onFailure
。如果是这样,userHash
将为null
Yes。好像从来没有人叫过它。。你知道为什么吗?