title: Unit test with LiveData Method getMainLooper in android.os.Looper not mocked
body: <p>I cannot make liveData.postValue working in while trying to make unit test. I have been checking in google for a solution and this is the code I have now. </p>
  
  <pre><code>public class ProjectListViewModelTest {
  
      GetProjectList getProjectList = Mockito.mock(GetProjectList.class);
      ProjectModel.Project project = new ProjectModel.Project("testing",
              "this is a test",
              "https://logo.jpg",
              new ProjectModel.Company("cat"),
              "20150404",
              "active");
      List&lt;ProjectModel.Project&gt; projects = Arrays.asList(project);
      ProjectModel.ProjectList projectsList = new ProjectModel.ProjectList(projects);
  
      ProjectsListViewModel projectsListViewModel;
  
      private PublishSubject&lt;ProjectModel.ProjectList&gt; projectsListPublishSubject = PublishSubject.create();
  
      @Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();
  
      @BeforeClass
      public static void setUpRxSchedulers() {
          Scheduler immediate = new Scheduler() {
              @Override
              public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
                  return super.scheduleDirect(run, 0, unit);
              }
  
              @Override
              public Scheduler.Worker createWorker() {
                  return new ExecutorScheduler.ExecutorWorker(Runnable::run);
              }
          };
  
          RxJavaPlugins.setInitIoSchedulerHandler(scheduler -&gt; immediate);
          RxJavaPlugins.setInitComputationSchedulerHandler(scheduler -&gt; immediate);
          RxJavaPlugins.setInitNewThreadSchedulerHandler(scheduler -&gt; immediate);
          RxJavaPlugins.setInitSingleSchedulerHandler(scheduler -&gt; immediate);
          RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -&gt; immediate);
      }
  
      @Before
      @Throws(exceptionClasses = Exception.class)
      public void setUp() {
          MockitoAnnotations.initMocks(this);
          projectsListViewModel = new ProjectsListViewModel(getProjectList);
          when(getProjectList.execute()).thenReturn(projectsListPublishSubject.take(1).singleOrError());
      }
  
      @Test
      public void testExecuteGetProjectsListSuccess() {
          LiveData&lt;List&lt;ProjectModel.MapProject&gt;&gt; liveData = projectsListViewModel.getLiveData();
          ProjectModel.MapProject expectedResult = new ProjectModel.MapProject(
                  "testing", "this is a test", "https://logo.jpg",
                  "cat", "2015-04-04", "active");
          projectsListViewModel.getProjects();
          projectsListPublishSubject.onNext(projectsList);
          Assert.assertEquals(expectedResult, liveData.getValue().get(0));
      }
  
      @After
      public void tearDownClass(){
          RxAndroidPlugins.reset();
      }
  </code></pre>
  
  <p>The code that I have in <code>setUpRxSchedulers</code> is mandatory in order to avoid the same error (<code>Method getMainLooper in android.os.Looper not mocked</code>) with Rx. But I cannot solve this error that I get when calling liveData.post(projectList). In all the forums that I checked they say that with <code>@Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();</code> the problem should be solved. But is not my case.</p>
  
  <p>I put here the viewmodel as well in case it can help:</p>
  
  <pre><code>public class ProjectsListViewModel extends ViewModel {
  
      GetProjectList getProjectList;
      MutableLiveData&lt;List&lt;ProjectModel.MapProject&gt;&gt; liveData = new MutableLiveData&lt;&gt;();
  
      public ProjectsListViewModel(GetProjectList getProjectList){
          this.getProjectList = getProjectList;
      }
  
      public LiveData&lt;List&lt;ProjectModel.MapProject&gt;&gt; getLiveData(){
          return liveData;
      }
  
      public void getProjects(){
          getProjectList.execute()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
                  .map(ProjectModel.ProjectList::getProjects)
                  .toObservable().flatMapIterable(projects -&gt; projects)
                  .map(project -&gt; project.convertToMapProject()).toList()
          .subscribe(projectsList -&gt;
              liveData.setValue(projectsList));
      }
  
  }
  </code></pre>