title: verify method invocation in AsyncTask
body: <p>I'm running Android Junit tests with Mockito. I'm testing an activity that creates an AsyncTask, and I want to verify that a method was called in the <code>AsyncTask</code>, but I get an error:</p>
  
  <pre><code>Wanted but not invoked
  </code></pre>
  
  <p>The test case looks like this:</p>
  
  <pre><code>@Test
  public void testFoo(){
  
  // Mocks have been initialized
  
  myActivity.onCreate(null);
  
  verify(myMock).myMethod(params);
  }
  </code></pre>
  
  <p>If I run the test like this, I get an error</p>
  
  <pre><code>Wanted but not invoked
  </code></pre>
  
  <p>The reason I get the error is because <code>myMethod</code> is being invoked in the AsyncTask, but <code>verify(...)</code> is being called before <code>myMethod</code> is invoked.</p>
  
  <p>To prove that this is correct, I put a pause inside the test like this </p>
  
  <pre><code>@Test
  public void testFoo(){
  
  // Mocks have been initialized
  
  myActivity.onCreate(null);
  
  thread.sleep(1000); // sleep so the AsyncTask can finish before calling verify
  
  verify(myMock).myMethod(params);
  }
  </code></pre>
  
  <p>and the test passed.</p>
  
  <p>Obviously, I don't want the thread to actually sleep. How can I properly make sure that verify is called after my <code>AsyncTask</code> completes?</p>