每当我在我的 Android 测试项目中运行所有测试时,我的一个测试永远不会完成。使用 LogCat,我将问题缩小到getActivity()
方法。我也Log.d()
输入了ViewAllSongsActivity.onCreate()
,但该消息从未打印出来。当我自己运行时ViewAllSongsActivityTests
,测试通过了。下面是测试的代码:
public class ViewAllSongsActivityTests extends ActivityInstrumentationTestCase2<ViewAllSongsActivity> {
public ViewAllSongsActivityTests() {
super(ViewAllSongsActivity.class);
}
public void testSongsAreLoaded() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Activity activity = this.getActivity(); // this never returns
ListView songList = (ListView) activity.findViewById(R.id.allSongsList);
assertTrue(songList.getCount() > 0);
}
}