Click Testing on Android ListView with ActivityUnitTestCase

I have implemented a ListView that launches a new Activity when a list item is clicked. When I test it manually, it works fine. But when I try to run an automatic test using ActivityUnitTestCase , I get a NullPointerException as if the ListView was empty.

MainActivity (partial):

 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); } }); } } 

Test code failed:

 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")); } 

Both test methods do not work, because listView.getChildAt(0) returns null . Why is the ListView empty? How can I get him to update himself with the right children?

+3
android android-activity android-listview unit-testing android-testing
Aug 31 '13 at 10:23
source share
2 answers

According to the documentation , If you prefer a functional test, you should use ActivityInstrumentationTestCase2. To perform a click, it works better. @Fewe's answer is right, you need to wait until the list is drawn, to wait for the fragment to load, you can use getInstrumentation (). WaitForIdleSync ();

Try something like this.

 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(); 

Then you can check the test, for example, if a fragment has been downloaded.

  mFragment frag = mActivity.getFragment(); assertNotNull("Fragment was not loaded", frag); 
+1
May 27 '14 at 17:38
source share

This is because the listview is not yet drawn. You must wait for it to be drawn before trying to access its cells.

0
Nov 15 '13 at 15:42
source share



All Articles