title: Android AsyncTask testing with Android Test Framework
body: <p>I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework. </p>
  
  <p>It works just fine when I instantiate and execute it in normal application. 
  However when it's executed from any of Android Testing framework classes (i.e. <strong><em>AndroidTestCase</em></strong>, <strong><em>ActivityUnitTestCase</em></strong>, <strong><em>ActivityInstrumentationTestCase2</em></strong> etc) it behaves strangely: </p>
  
  <ul>
  <li>It executes <code>doInBackground()</code> method correctly </li>
  <li>However it doesn't invokes any of its notification methods (<code>onPostExecute()</code>, <code>onProgressUpdate()</code>, etc) -- just silently ignores them without showing any errors.</li>
  </ul>
  
  <p>This is very simple AsyncTask example:</p>
  
  <pre><code>package kroz.andcookbook.threads.asynctask;
  
  import android.os.AsyncTask;
  import android.util.Log;
  import android.widget.ProgressBar;
  import android.widget.Toast;
  
  public class AsyncTaskDemo extends AsyncTask&lt;Integer, Integer, String&gt; {
  
  AsyncTaskDemoActivity _parentActivity;
  int _counter;
  int _maxCount;
  
  public AsyncTaskDemo(AsyncTaskDemoActivity asyncTaskDemoActivity) {
      _parentActivity = asyncTaskDemoActivity;
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
      _parentActivity._progressBar.setVisibility(ProgressBar.VISIBLE);
      _parentActivity._progressBar.invalidate();
  }
  
  @Override
  protected String doInBackground(Integer... params) {
      _maxCount = params[0];
      for (_counter = 0; _counter &lt;= _maxCount; _counter++) {
          try {
              Thread.sleep(1000);
              publishProgress(_counter);
          } catch (InterruptedException e) {
              // Ignore           
          }
      }
  }
  
  @Override
  protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      int progress = values[0];
      String progressStr = "Counting " + progress + " out of " + _maxCount;
      _parentActivity._textView.setText(progressStr);
      _parentActivity._textView.invalidate();
  }
  
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);
      _parentActivity._progressBar.setVisibility(ProgressBar.INVISIBLE);
      _parentActivity._progressBar.invalidate();
  }
  
  @Override
  protected void onCancelled() {
      super.onCancelled();
      _parentActivity._textView.setText("Request to cancel AsyncTask");
  }
  
  }
  </code></pre>
  
  <p>This is a test case. Here <em>AsyncTaskDemoActivity</em> is a very simple Activity providing UI for testing AsyncTask in mode:</p>
  
  <pre><code>package kroz.andcookbook.test.threads.asynctask;
  import java.util.concurrent.ExecutionException;
  import kroz.andcookbook.R;
  import kroz.andcookbook.threads.asynctask.AsyncTaskDemo;
  import kroz.andcookbook.threads.asynctask.AsyncTaskDemoActivity;
  import android.content.Intent;
  import android.test.ActivityUnitTestCase;
  import android.widget.Button;
  
  public class AsyncTaskDemoTest2 extends ActivityUnitTestCase&lt;AsyncTaskDemoActivity&gt; {
  AsyncTaskDemo _atask;
  private Intent _startIntent;
  
  public AsyncTaskDemoTest2() {
      super(AsyncTaskDemoActivity.class);
  }
  
  protected void setUp() throws Exception {
      super.setUp();
      _startIntent = new Intent(Intent.ACTION_MAIN);
  }
  
  protected void tearDown() throws Exception {
      super.tearDown();
  }
  
  public final void testExecute() {
      startActivity(_startIntent, null, null);
      Button btnStart = (Button) getActivity().findViewById(R.id.Button01);
      btnStart.performClick();
      assertNotNull(getActivity());
  }
  
  }
  </code></pre>
  
  <p>All this code is working just fine, except the fact that AsyncTask doesn't invoke it's notification methods when executed by within Android Testing Framework. Any ideas?</p>