title: How to make runBlocking wait for "all threads" in Unit Testing?
body: <p>I'm trying to write a unit test for <code>getUser()</code> function:</p>
  
  <pre><code>fun getUser(userId:Int) {
      // some code...
  
      launchDataOperation { api.getUser(userId) }
  }
  </code></pre>
  
  <p>The issue I can see (but don't know how to resolve in a clean way) is  <code>launchDataOperation(...)</code> creates a coroutine to call suspend function <code>api.getUser</code>:</p>
  
  <pre><code>fun launchDataOperation(block: suspend () -&gt; Unit): Job {
      return coroutineScope.launch {
          try {
              setLoading(true)
              block()
          } catch (error: NetworkConnectionException) {
              setError(Errors.NETWORK_CONNECTION_ERROR)
          } catch (error: DataOperationException) {
              setMessage(error.getErrors())
          } finally {
              setLoading(false)
          }
      }
  }
  </code></pre>
  
  <p>This is my failing test:</p>
  
  <pre><code>@Test
  fun `error message is displayed if api error occurred`() {
      val exception = DataOperationException(listOf("FATAL ERROR"))
  
      runBlocking {
          `when`(api.getUser(anyInt())).thenAnswer { throw exception }
  
          subject.getUser(userId)
  
          // execution fails if the below line is commented
          // this is not the correct way to wait for other coroutines
          delay(500)  // wait for 500ms till the other coroutines finish
  
          assertThat(subject.messages.value).isEqualTo(exception.getErrors())
      }
  }
  </code></pre>