'Kotlin Coroutines Unit Testing

Im trying to test this suspend function:

suspend fun <T> getResult(call: suspend () -> Response<T>): Resource<T> {

  val response = call()
  val body = response.body()
  val code = response.code()

  if (response.isSuccessful) {

    Log.d(Cybrid.instance.tag, "Data: ${response.code()} - ${response.body()}")
    return Resource.success(body!!, code)

  } else if (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) {

    Cybrid.instance.let { cybrid ->
      cybrid.listener.let {
        cybrid.invalidToken = true
        it?.onTokenExpired()
      }
    }
    Log.e(Cybrid.instance.tag, "Error - Something wrong with TOKEN : ${response.code()} ${response.message()}")
    return Resource.error(response.message(), code=response.code())
  } else {
    Log.e(Cybrid.instance.tag, "Error - Other: ${response.code()} ${response.message()} :: ${response.raw()}")
    return Resource.error(message = response.message(), data= response.body(), code= response.code())
  }
}

With this Unit Test Case, all it cover except line 13 , I dont know how to cover the line!!!

@ExperimentalCoroutinesApi
@Test
fun get400ErrorServerTest() = runBlocking {

    Cybrid.instance.setBearer("Bearer")
    val pricesService = AppModule.getClient().createService(PricesApi::class.java)
    val result = getResult { pricesService.listPrices() }

    Assert.assertNotNull(result)
    Assert.assertEquals(result.code, 400)
    Assert.assertNull(result.data)
}

The coverage report says:

Coverage

Some idea to get coverage for line 13 ??? Thnanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source