title: AsyncTask onPostExecute() not called in Unit Test Case
body: <p>I've seen a bunch of posts related to this, but none seem to have the same issue I'm getting. GetBusinessRulesTask extends AsyncTask. When I execute this in a unit test case the onPostExecute() never gets called. However, if I use the real client code then onPostExecute() is called everytime. Not sure what I'm doing wrong here.</p>
  
  <p>Test Case:</p>
  
  <pre><code>package com.x.android.test.api;
  
  import java.util.concurrent.CountDownLatch;
  
  import android.test.ActivityInstrumentationTestCase2;
  import android.test.UiThreadTest;
  import android.widget.Button;
  
  import com.x.android.api.domain.businessrule.BusinessRules;
  import com.x.android.api.exception.NetworkConnectionException;
  import com.x.android.api.tasks.GetBusinessRulesTask;
  import com.x.android.test.activity.SimpleActivity;
  
   public class GetBusinessRulesTaskTest
      extends
          ActivityInstrumentationTestCase2&lt;SimpleActivity&gt; {
  SimpleActivity mActivity;
  Button mButton;
  
  public GetBusinessRulesTaskTest() {
      super("com.x.android.test.activity", SimpleActivity.class);
  }
  
  @Override
  protected void setUp() throws Exception {
      super.setUp();
      mActivity = this.getActivity();
      mButton = (Button) mActivity
              .findViewById(com.x.android.test.activity.R.id.b1);
  }
  
  public void testPreconditions() {
      assertNotNull(mButton);
  }
  
  @UiThreadTest
  public void testCallBack() throws Throwable {
      final CountDownLatch signal = new CountDownLatch(1);
      final GetBusinessRulesTask task = (GetBusinessRulesTask) new GetBusinessRulesTask(
              new GetBusinessRulesTask.Receiver&lt;BusinessRules&gt;() {
                  @Override
                  public void onReceiveResult(BusinessRules rules, Exception e) {
                      assertNotNull(rules);
                      assertNull(e);
                      signal.countDown();// notify the count down latch
                  }
              });
      task.start(mActivity.getApplicationContext());
      try {
          signal.await();// wait for callback
      } catch (InterruptedException e1) {
          fail();
          e1.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>OnPostExecute:</p>
  
  <pre><code>@Override
  protected void onPostExecute(AsyncTaskResponse&lt;O&gt; response) {
      Log.d(TAG, "onPostExecuted");
      if (mReceiver != null) {
          mReceiver.onReceiveResult(response.getResponse(),   response.getException());
      }
  }
  </code></pre>
  
  <p>DoInBackground:</p>
  
  <pre><code>@Override
  protected AsyncTaskResponse&lt;O&gt; doInBackground(I... params) {
      Log.d(TAG, "doInBackgroundr");
      try {
          Uri uri = createUri(params);
          mBaseRequest = new GetLegacyRequest(uri);
          String json = mBaseRequest.executeRequest();
          O response = deserializeJson(json);
          Log.d(TAG, "Returning AsyncTaskResponse");
          return new AsyncTaskResponse&lt;O&gt;(response, null);
      } catch (Exception e) {
          Log.e(TAG, "Error", e);
          /*
          AsyncTaskResponse&lt;O&gt; maintenance = ReadBusinessControlledPropertiesTask.blockingCall(mServiceLocatorUrl);
          if(maintenance.getException() == null) {
              MaintenanceException mExcep = new MaintenanceException( maintenance.getResponse());
              if (mExcep.isUnderMaintenance())
                  return new AsyncTaskResponse(null,mExcep);
          }*/
          return new AsyncTaskResponse&lt;O&gt;(null, e);
      }
  }
  </code></pre>
  
  <p>Start method()</p>
  
  <pre><code>public AsyncTask&lt;Void, Void, AsyncTaskResponse&lt;BusinessRules&gt;&gt; start(
          Context context) throws NetworkConnectionException {
      super.start(context);
      Log.d(TAG, "start");
      return execute();
  }
  </code></pre>
  
  <p>FOUND THE ISSUE. Don't make your AsyncTask final and put it inside the runnable. </p>
  
  <p>The fix:</p>
  
  <pre><code>public void testCallBack() throws Throwable {
      final CountDownLatch signal = new CountDownLatch(1);
      // Execute the async task on the UI thread! THIS IS KEY!
      runTestOnUiThread(new Runnable() {
      @Override
          public void run() {
              try {
                GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiver&lt;BusinessRules&gt;() {
                              @Override
                              public void onReceiveResult(
                                      BusinessRules rules, Exception e) {
                                  assertNotNull(rules);
                                  assertNull(e);
                                  signal.countDown();// notify the count downlatch
                              }
                          });
                  task.start(mActivity.getApplicationContext());
              } catch (Exception e) {
                  Log.e(TAG, "ERROR", e);
                  fail();
              }
          }
      });
      try {
          signal.await();// wait for callback
      } catch (InterruptedException e1) {
          fail();
          e1.printStackTrace();
      }
  }
  </code></pre>