title: Unit test ViewModel using repository pattern and LiveData
body: <p>I want to write a unit test for my viewmodel class :</p>
  <pre><code>class MainViewModel(
      repository: ShowRepository
  ) : ViewModel() {
  
      private val _shows = repository.shows
      val shows: LiveData&lt;MyResult&lt;List&lt;Show&gt;&gt;&gt;
          get() = _shows
  }
  </code></pre>
  <p>Here is my repository class :</p>
  <pre><code>class ShowRepository(
      private val dao: ShowDao,
      private val api: TVMazeService,
      private val context: Context
  ) {
  
      /**
       * A list of shows that can be shown on the screen.
       */
      val shows = resultLiveData(
          databaseQuery = {
              Transformations.map(dao.getShows()) {
                  it.asDomainModel()
              }
          },
          networkCall = { refreshShows() })
  
      /**
       * Refresh the shows stored in the offline cache.
       */
      suspend fun refreshShows(): MyResult&lt;List&lt;Show&gt;&gt; =
          try {
              if (isNetworkAvailable(context)) {
                  val shows = api.fetchShowList().await()
                  dao.insertAll(*shows.asDatabaseModel())
                  MyResult.success(shows)
              } else {
                  MyResult.error(context.getString(R.string.failed_internet_msg))
              }
          } catch (err: HttpException) {
              MyResult.error(context.getString(R.string.failed_loading_msg))
          } catch (err: UnknownHostException) {
              MyResult.error(context.getString(R.string.failed_unknown_host_msg))
          } catch (err: SocketTimeoutException) {
              MyResult.error(context.getString(R.string.failed_socket_timeout_msg))
          }
  }
  </code></pre>
  <p>And here is my Dao class :</p>
  <pre><code>@Dao
  interface ShowDao {
  
      /**
       * Select all shows from the shows table.
       *
       * @return all shows.
       */
      @Query(&quot;SELECT * FROM databaseshow&quot;)
      fun getShows(): LiveData&lt;List&lt;DatabaseShow&gt;&gt;
  }
  </code></pre>
  <p>Here is my unit test :</p>
  <pre><code>@ExperimentalCoroutinesApi
  class MainViewModelTest {
  
      private lateinit var viewModel: MainViewModel
      private lateinit var repository: ShowRepository
      private val api: TVMazeService = mock()
      private val dao: ShowDao = mock()
      private val context: Context = mock()
  
      @Test
      fun fetch() {
          val observer1: Observer&lt;List&lt;DatabaseShow&gt;&gt; = mock()
          dao.getShows().observeForever(observer1)
  
          repository = ShowRepository(dao, api, context)
  
          val observer2: Observer&lt;MyResult&lt;List&lt;Show&gt;&gt;&gt; = mock()
          repository.shows.observeForever(observer2)
  
          viewModel = MainViewModel(repository)
  
          val observer3: Observer&lt;MyResult&lt;List&lt;Show&gt;&gt;&gt; = mock()
          viewModel.shows.observeForever(observer3)
  
          verify(viewModel).shows
      }
  }
  </code></pre>
  <p>But I receive following exception :</p>
  <pre><code>java.lang.NullPointerException
      at com.android.sample.tvmaze.viewmodel.MainViewModelTest.fetch(MainViewModelTest.kt:39)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
      at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
      at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  </code></pre>
  <p>I appreciate if you give me any guideline.</p>