title: How can I test coroutine flow response in JUnit4?
body: <p>This is my first time that I test with Flow.</p>
  <p>I used to write test with LiveData. And I used getOrAwaitValue() like this:</p>
  <pre><code>@VisibleForTesting(otherwise = VisibleForTesting.NONE)
  fun &lt;T&gt; LiveData&lt;T&gt;.getOrAwaitValue(
      time: Long = 2,
      timeUnit: TimeUnit = TimeUnit.SECONDS,
      afterObserve: () -&gt; Unit = {}
  ): T {
      var data: T? = null
      val latch = CountDownLatch(1)
      val observer = object : Observer&lt;T&gt; {
          override fun onChanged(o: T?) {
              data = o
              latch.countDown()
              this@getOrAwaitValue.removeObserver(this)
          }
      }
      this.observeForever(observer)
  
      try {
          afterObserve.invoke()
  
          // Don't wait indefinitely if the LiveData is not set.
          if (!latch.await(time, timeUnit)) {
              throw TimeoutException(&quot;LiveData value was never set.&quot;)
          }
  
      } finally {
          // need to remove observer
          this.removeObserver(observer)
      }
  
      @Suppress(&quot;UNCHECKED_CAST&quot;)
      return data as T
  }
  </code></pre>
  <p>So, I converted Flow to LiveData to use getOrAwaitValue</p>
  <pre><code>    @Test
      fun fetchStudentList_success_returnStudentList() {
          // given
          coEvery { studentRepo.fetchStudentList() } returns flow { studentResponse }
  
          // then
          assertEquals(carriersResponse.data.results.toString(), mainViewModel.studentListState.asLiveData().getOrAwaitValue())
      }
  </code></pre>
  <p>But it always fail since Actual value returns reference value like this:</p>
  <pre><code>Expected: [&quot;Jack&quot;, &quot;Evan&quot;, &quot;Alan&quot;]
  Actual: com.example.ResultState$UnInitialize@299b9851
  </code></pre>
  <p>How can I solve this issue?</p>