title: Unit test the new Kotlin coroutine StateFlow
body: <p>Recently, the class <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/" rel="nofollow noreferrer"><code>StateFlow</code> was introduced</a> as part of Kotlin coroutines.</p>
  <p>I'm currently trying it and encountered an issue while trying to unit test my <em>ViewModel</em>. What I want to achieve: testing that my <em>StateFlow</em> is receiving all the state values in the correct order in my <em>ViewModel</em>.</p>
  <p>My code is as follows.</p>
  <p>ViewModel:</p>
  <pre class="lang-kotlin prettyprint-override"><code>class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() {
  
  val userWallet: StateFlow&lt;State&lt;UserWallets&gt;&gt; get() = _userWallets
  private val _userWallets: MutableStateFlow&lt;State&lt;UserWallets&gt;&gt; =
          MutableStateFlow(State.Init)
  
  fun getUserWallets() {
      viewModelScope.launch {
          getUserWallets.getUserWallets()
              .onStart { _userWallets.value = State.Loading }
              .collect { _userWallets.value = it }
      }
  }
  </code></pre>
  <p>My test:</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Test
  fun `observe user wallets ok`() = runBlockingTest {
      Mockito.`when`(api.getAssetWallets()).thenReturn(TestUtils.getAssetsWalletResponseOk())
      Mockito.`when`(api.getFiatWallets()).thenReturn(TestUtils.getFiatWalletResponseOk())
  
      viewModel.getUserWallets()
          
      val res = arrayListOf&lt;State&lt;UserWallets&gt;&gt;()
      viewModel.userWallet.toList(res) //doesn't works
  
      Assertions.assertThat(viewModel.userWallet.value is State.Success).isTrue() //works, last value enmited
  }
  </code></pre>
  <p>Accessing the last value emitted works. But what I want to test is that all the emitted values are emitted in the correct order.</p>
  <p>With this piece of code: <code>viewModel.userWallet.toList(res)</code> I'm getting the following error:</p>
  <pre><code>java.lang.IllegalStateException: This job has not completed yet
      at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1189)
      at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
      at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
      at WalletViewModelTest.observe user wallets ok(WalletViewModelTest.kt:52)
  ....
  </code></pre>
  <p>I guess I'm missing something obvious. But not sure why as I'm just getting started with coroutines and <em>Flow</em> and this error seems to happen when not using <code>runBlockingTest</code>, which I use already.</p>
  <p>EDIT:</p>
  <p>As a temporary solution, I'm testing it as a live data:</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Captor
  lateinit var captor: ArgumentCaptor&lt;State&lt;UserWallets&gt;&gt;
      
  @Mock
  lateinit var walletsObserver: Observer&lt;State&lt;UserWallets&gt;&gt;
  
  @Test
  fun `observe user wallets ok`() = runBlockingTest {
      viewModel.userWallet.asLiveData().observeForever(walletsObserver)
      
      viewModel.getUserWallets()
      captor.run {
          Mockito.verify(walletsObserver, Mockito.times(3)).onChanged(capture())
          Assertions.assertThat(allValues[0] is State.Init).isTrue()
          Assertions.assertThat(allValues[1] is State.Loading).isTrue()
          Assertions.assertThat(allValues[2] is State.Success).isTrue()
      }
  }
  </code></pre>