title: How to test a Firebase task on Android?
body: <p>Now I am trying to test it using JUnit4, but I am getting </p>
  
  <pre><code>java.lang.IllegalStateException:
      Must not be called on the main application thread
  
  at com.google.android.gms.common.internal.zzab.zzhj(Unknown Source)
  at com.google.android.gms.common.internal.zzab.zzate(Unknown Source)
  at com.google.android.gms.tasks.Tasks.await(Unknown Source)
  at com.example.tasks.GetUserProfileTest.then(GetUserProfileTest.java:18)
      &lt;27 internal calls&gt;
  
  Process finished with exit code 255
  </code></pre>
  
  <p>I've created a <a href="https://developers.google.com/android/reference/com/google/android/gms/tasks/Continuation" rel="nofollow noreferrer">Continuation</a> task that performs an operation on <a href="https://firebase.google.com/docs/database/android/start/" rel="nofollow noreferrer">Firebase Realtime Database</a>.</p>
  
  <pre><code>public class GetUserProfile implements Continuation&lt;String, Task&lt;Profile&gt;&gt;,
          ValueEventListener {
  
      private TaskCompletionSource&lt;Profile&gt; mTaskCompletionSource;
      private DatabaseReference mDatabase;
  
      public GetUserProfile() {
          mTaskCompletionSource = new TaskCompletionSource();
          mDatabase = FirebaseDatabase.getInstance().getReference();
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError) {
          mDatabase.removeEventListener(this);
          mTaskCompletionSource.setException(databaseError.toException());
      }
  
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          mDatabase.removeEventListener(this);
          mTaskCompletionSource.setResult(dataSnapshot.getValue(Profile.class));
      }
  
      @Override
      public Task&lt;Profile&gt; then(Task&lt;String&gt; task) {
          mDatabase.child(task.getResult()).addValueEventListener(this);
          return mTaskCompletionSource.getTask();
      }
  }
  </code></pre>
  
  <p>and unit test for it:</p>
  
  <pre><code>public class GetUserProfileTest {
      @Test
      public void then() throws Exception {
          Task&lt;Profile&gt; task = Tasks.&lt;String&gt;forResult("17354686546")
              .continueWithTask(new GetUserProfile());
  
          try {
              Profile profile = Tasks.await(task);
              assertEquals(profile.getEmail(), "john.connor@email.com");
          } catch (ExecutionException e) {
              fail(e.getMessage());
          } catch (InterruptedException e) {
              fail(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Is there a simple way of testing tasks without using <a href="https://stackoverflow.com/questions/33593865/user-mockito-to-unit-test-a-function-doing-async-task-in-my-case">handlers</a> or <a href="https://developer.android.com/reference/java/util/concurrent/CountDownLatch.html" rel="nofollow noreferrer"><code>CountDownLatch</code></a>?</p>