title: Why doesn't mocking work with AsyncTask?
body: <p>I'm testing an Android app using an ApplicationTestCase. I want to mock one of my AsyncTasks (example simplified to show the problem):</p>
  
  <pre><code>public class Foo extends AsyncTask&lt;Void,Void,Void&gt; {
    @Override
    protected Void doInBackground(Void... unused) {
      return null;
    }
  }
  </code></pre>
  
  <p>So to set up my tests, I did the following:</p>
  
  <pre><code>private Foo mockFoo;
  
  @Override
  protected void setUp() throws Exception {
    super.setUp()
  
    mockFoo = mock(Foo.class);
  
    createApplication();
  }
  </code></pre>
  
  <p>And then the actual test is the following:</p>
  
  <pre><code>public void testAsyncTaskMock() {
    mockFoo.execute();
    verify(mockFoo).execute();
  }
  </code></pre>
  
  <p>But I get an exception when <code>mockFoo.execute();</code> runs:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.AsyncTask$Status.ordinal()' on a null object reference
  </code></pre>
  
  <p><strong>Why is that technique of mocking the AsyncTask not working?</strong></p>
  
  <p>Note that removing <code>createApplication();</code> causes the problem to go away in this simple case, but for my actual testing I do need the application to be created.</p>