【发布时间】:2014-06-17 10:41:00
【问题描述】:
我想测试打开线程并返回回调的函数。 每次在第一个测试用例之后,第二个测试被挂起并且在日志中我看到 ApacheSDK(32004): Exception e:java.lang.NullPointerException。当我删除 ~/.android 文件并清理项目时,错误消失了,但这并不能解决问题。 我正在使用 CountDownLatch 使测试的线程等待。 如果我要评论其中一个测试,另一个测试运行没有任何问题。我真的不明白问题是在我的代码中还是在 ADT 中。
public class HelperTest extends ActivityInstrumentationTestCase2<MainActivity> {
@Test
public void test_A() throws Throwable {
TestAdInfoMock info = new TestAdInfoMock();
final CountDownLatch latch = new CountDownLatch(1);
when(util.execute()).thenReturn(X, Y, Z,
T);
final IRequest requestComplete = new IRequest() {
@Override
public void onReceiveInfoCompleted() {
assertNull(“message”, …);
assertNull("message", …);
latch.countDown();
}
};
//The requestData opens thread with AsyncTask
Helper.requestData(context, requestComplete);
boolean await = latch.await(2, TimeUnit.SECONDS);
assertTrue(await);
}
@Test
public void test_B() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
when(util.execute()).thenReturn(X, Y, Z,
T);
final IRequest requestComplete = new IRequest() {
@Override
public void onReceiveInfoCompleted() {
assertNull(“message”, …);
assertNull("message", …);
latch.countDown();
}
};
//The requestData opens thread with AsyncTask
Helper.requestData(context, requestComplete); //
boolean await = latch.await(2, TimeUnit.SECONDS);
assertTrue(await);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
clearPreferences();
}
void clearPreferences() {
SharedPreferences preferences = getActivity().getSharedPreferences("global", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.remove(KEY!);
editor.remove(KEY2);
editor.commit();
}
}
感谢任何建议和 cmets。
编辑:
为了找出问题,我开始进行原始分析:迭代。
当从 tearDown 方法中删除 clearPreferences() 方法时,所有测试工作正常。 但我仍然不明白为什么 clearPreferences 方法应该挂起测试。
【问题讨论】:
标签: android multithreading integration-testing mockito