3

我已经实现了一个在单击列表项时ListView启动一个新的。Activity当我手动测试它时,它工作得很好。但是当我尝试使用 进行自动化测试时ActivityUnitTestCase,我得到的NullPointerException好像ListView是空的。

MainActivity(部分):

public class MainActivity extends ListActivity {

  @Override
  protected void onResume() {
    super.onResume();

    String[] items = new String[] {"item 1", "item 2"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    setListAdapter(adapter);

    getListView().setOnItemClickListener(new OnItemClickListener () {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView clickedItem = (TextView) view;
        CharSequence clickedItemText = clickedItem.getText(); // throws a NullPointerException when running testWithPerformItemClick()!
        Intent intent = new Intent(MainActivity.this, ItemDisplayActivity.class);
        intent.putExtra("parameter", clickedItemText);
        startActivity(intent);
      }
    });
  }
}

失败的测试代码:

public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {

  ListView listView;
  View child0;

  public MainActivityTest() {
    super(MainActivity.class);
  }

  private void setUpTest() {
    MainActivity activity = startActivity(new Intent(), null, null);
    getInstrumentation().callActivityOnStart(activity);
    getInstrumentation().callActivityOnResume(activity);
    listView = (ListView) activity.findViewById(android.R.id.list);
    child0 = listView.getChildAt(0); // returns null!
  }

  public void testWithPerformClick() {
    setUpTest();

    child0.performClick(); // throws a NullPointerException!

    Intent startedIntent = getStartedActivityIntent();
    assertEquals("item 1", startedIntent.getStringExtra("parameter"));
  }

  public void testWithPerformItemClick() {
    setUpTest();
    long itemId = listView.getAdapter().getItemId(0);

    listView.performItemClick(child0, 0, itemId); // throws a NullPointerException!

    Intent startedIntent = getStartedActivityIntent();
    assertEquals("item 1", startedIntent.getStringExtra("parameter"));
  }

两种测试方法都失败,因为listView.getChildAt(0)返回null. 为什么是ListView空的?我怎样才能强迫它用正确的孩子更新自己?

4

2 回答 2

1

根据文档,如果您更喜欢功能测试,您应该使用 ActivityInstrumentationTestCase2。要执行点击,效果会更好。@fewe 的答案是正确的,您必须等待绘制列表,等待加载片段,您可以使用 getInstrumentation().waitForIdleSync();

尝试这样的事情。

public class ActivityTests extends
    ActivityInstrumentationTestCase2<Activity>

final ListView list = (ListView) mActivity.findViewById(R.id.listId);
         assertNotNull("The list was not loaded", list);
         getInstrumentation().runOnMainSync(new Runnable() {
             @Override
             public void run() {

                 list.performItemClick(list.getAdapter().getView(position, null, null),
                         position, list.getAdapter().getItemId(position));
             }

         });

 getInstrumentation().waitForIdleSync();

然后你不能测试,例如,如果一个片段被加载。

  mFragment frag = mActivity.getFragment();
  assertNotNull("Fragment was not loaded", frag);
于 2014-05-27T17:38:02.823 回答
0

这是因为列表视图尚未绘制。在尝试访问其单元格之前,您应该等待它被绘制

于 2013-11-15T15:42:09.527 回答