Android JUnit测试-如何模拟Google API调用

时间:2018-12-12 06:30:39

标签: android junit in-app-billing

我正在学习JUNIT测试,并想为购买创建测试。 我不明白如何才能不伪造Google API函数querySkuDetailsAsync的结果,该函数返回responseCodeskuDetails的列表,然后将其传递给calculatePrice()。如何在测试功能中获得这些结果?

fun getPriceAndProductId(
        obj: Object,
        listener: (price: Double, productId: String) -> Unit
) {
     val skus = listOf(
            sku1,
            sku2,
            sku3)

     billingManager.querySkuDetailsAsync(
            BillingClient.SkuType.INAPP,
            skus,
            SkuDetailsResponseListener { responseCode, skuDetailsList ->
                if (responseCode == BillingClient.BillingResponse.OK) {
                    val price = calculatePrice(...)
                    val productId = calculateProductId(...)

                } else {
                    LogUtil.debug("...", "Unable to get SKUS: $responseCode")
                }
            })
}

JUnit:

@Test
fun testProductPrice() {
    val skuDetailsList = /* somehow call google API and get the skus */
    assertEquals(objectClass.calculatePrice(skuDetailsList), "0.0")
}

0 个答案:

没有答案