title: How to correctly mock ViewModel on androidTest
body: <p>I'm currently writing some UI unit tests for a fragment, and one of these <code>@Test</code> is to see if a list of objects is correctly displayed, this is <strong>not an integration</strong> test, therefore I wish to <strong>mock</strong> the <code>ViewModel</code>.</p>
  
  <p>The fragment's vars:</p>
  
  <pre><code>class FavoritesFragment : Fragment() {
  
      private lateinit var adapter: FavoritesAdapter
      private lateinit var viewModel: FavoritesViewModel
      @Inject lateinit var viewModelFactory: FavoritesViewModelFactory
  
      (...)
  </code></pre>
  
  <p>Here's the code:</p>
  
  <pre><code>@MediumTest
  @RunWith(AndroidJUnit4::class)
  class FavoritesFragmentTest {
  
      @Rule @JvmField val activityRule = ActivityTestRule(TestFragmentActivity::class.java, true, true)
      @Rule @JvmField val instantTaskExecutorRule = InstantTaskExecutorRule()
  
      private val results = MutableLiveData&lt;Resource&lt;List&lt;FavoriteView&gt;&gt;&gt;()
      private val viewModel = mock(FavoritesViewModel::class.java)
  
      private lateinit var favoritesFragment: FavoritesFragment
  
      @Before
      fun setup() {
          favoritesFragment = FavoritesFragment.newInstance()
          activityRule.activity.addFragment(favoritesFragment)
          `when`(viewModel.getFavourites()).thenReturn(results)
      }
  
      (...)
  
      // This is the initial part of the test where I intend to push to the view
      @Test
      fun whenDataComesInItIsCorrectlyDisplayedOnTheList() {
          val resultsList = TestFactoryFavoriteView.generateFavoriteViewList()
          results.postValue(Resource.success(resultsList))
  
          (...)
      }
  </code></pre>
  
  <p>I was able to mock the <code>ViewModel</code> but of course, that's not the same <code>ViewModel</code> created inside the <code>Fragment</code>.</p>
  
  <p>So my question really, has someone done this successfully or has some pointers/references that might help me out?</p>
  
  <ul>
  <li><p>Also, I've tried looking into the <a href="https://github.com/googlesamples/android-architecture-components" rel="noreferrer">google-samples</a> but with no luck.</p></li>
  <li><p><strong>For reference, the project can be found here:</strong> <a href="https://github.com/JoaquimLey/transport-eta/" rel="noreferrer">https://github.com/JoaquimLey/transport-eta/</a></p></li>
  </ul>