Skip to content Skip to sidebar Skip to footer

Testing A Click On An Android Listview With Activityunittestcase

I have implemented a ListView that starts a new Activity when a list item is clicked. When I test it manually, it works perfectly. But when I try to do an automated test with Activ

Solution 1:

According to the documentation, If you prefer a functional test you should use ActivityInstrumentationTestCase2. To perform a click it works better. The @fewe answer is right, you have to wait the list to be drawn, to wait the fragment to be loaded you can use getInstrumentation().waitForIdleSync();

Try something like this.

publicclassActivityTestsextendsActivityInstrumentationTestCase2<Activity>

finalListViewlist= (ListView) mActivity.findViewById(R.id.listId);
         assertNotNull("The list was not loaded", list);
         getInstrumentation().runOnMainSync(newRunnable() {
             @Overridepublicvoidrun() {

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

         });

 getInstrumentation().waitForIdleSync();

Then you cant test, for example, if a fragment was loaded.

mFragmentfrag= mActivity.getFragment();
  assertNotNull("Fragment was not loaded", frag);

Solution 2:

This is because the listview isn't drawn yet. You should wait for it to be drawn before trying to access its cells

Post a Comment for "Testing A Click On An Android Listview With Activityunittestcase"