接上次的问题:大家都是怎么实现 android 异步接口测试的呢
根据大家的提示,和网上的各种资料,将代码修改为
public class Test extends InstrumentationTestCase {
private Activity mActivity;
private Context mApplicationContext;
private int error_code = -1;
private String message;
private static final String TAG = "MyTest";
@Override
protected void setUp() throws Exception {
super.setUp();
Intent intent = new Intent();
intent.setClassName("xxx.xxx.com.test", MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity = (MainActivity) getInstrumentation().startActivitySync(intent);
mApplicationContext = mActivity.getApplicationContext();
}
@Override
protected void tearDown() throws Exception {
mActivity.finish();
try {
super.tearDown();
} catch (Exception e) {
e.printStackTrace();
}
super.tearDown();
}
public void testLogin() throws Throwable{
final CountDownLatch signal = new CountDownLatch(1);
//在UI线程中执行异步操作
runTestOnUiThread(new Runnable() {
@Override
public void run() {
//我要测试的方法
A.getInstance().login("ffff", 11, "", "test", new MyManager.CallBack() {
@Override
public void finish(int code, String msg) {
error_code = code;
message = msg;
signal.countDown();
Log.d(TAG, message);
Log.d(TAG, error_code + "");
}
});
}
});
//signal.await(10, TimeUnit.SECONDS);
signal.await();
assertEquals(1, error_code);
}
}
执行测试,测试方法一直转圈,且 error_code 一直是-1,中间的 Log.d 也没有被执行到