title: Using ActivityInstrumentationTestCase2 cannot seem to test ListView contents
body: <p>I have the following test code. I am trying to test whether a list is being populated from SQLite database.</p>
  
  <pre><code>public class ViewIssuesActivityTest extends BaseActivityTest&lt;ViewIssuesActivity&gt;{
  
    private List&lt;Issue&gt; issues;
  
    public ViewIssuesActivityTest() {
      super(ViewIssuesActivity.class);
    }
  
    public void setUp() throws Exception {
      super.setUp();
      issues = new ArrayList&lt;Issue&gt;();
      issues.add(new Issue("Trial","Desc","Location","ImagePath"));
      IssueRepository issueRepository = mock(IssueRepository.class);
  
  
      doAnswer(new Answer&lt;Object&gt;() {
          @Override
          public List&lt;Issue&gt; answer(InvocationOnMock invocation) throws Throwable {
              return issues;
          }
      }).when(issueRepository).getIssues();
      activity.setIssueRepository(issueRepository);
  
    }
  
    public void testNumberOfIssuesRecorded() {
      ListView listView = (ListView) activity.findViewById(android.R.id.list);
      assertEquals(1, listView.getCount());
    }
  }
  </code></pre>
  
  <p>My BaseActivityTest code is:</p>
  
  <pre><code>public class BaseActivityTest&lt;T extends Activity&gt; extends ActivityInstrumentationTestCase2&lt;T&gt; {
  
   protected T activity;
  
   public BaseActivityTest(Class&lt;T&gt; activityClass) {
       super(activityClass);
   }
  
   @Override
   protected void setUp() throws Exception {
       activity = getActivity();
   }
  }
  </code></pre>
  
  <p>My ViewIssuesActivity is as follows:</p>
  
  <pre><code>public class ViewIssuesActivity extends ListActivity{
  
   private IssueRepository issueRepository;
  
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
  
   super.onCreate(savedInstanceState);
   setContentView(com.change.kranti.R.layout.issues);
   issueRepository = new IssueRepository(getApplicationContext());
  
   List&lt;Issue&gt; issues = new ArrayList&lt;Issue&gt;();
   issues = issueRepository.getIssues();
   ArrayAdapter&lt;Issue&gt; adapter = new ArrayAdapter&lt;Issue&gt;(this,
    android.R.layout.simple_list_item_1, issues);
  
   setListAdapter(adapter);
   }
  
  }
  </code></pre>
  
  <p>The issue is I get error: expected&lt;1> got &lt;0></p>
  
  <p>I think the issue is the onCreate method is getting called before the issueRepository is created.
  I want to mock the IssueRepository and test my list getting populated. </p>
  
  <p>What is the issue with my code or is there a better way to test this functionality.</p>
  
  <p>Any help will be appreciated.</p>