我正在学习JUNIT测试,并想为购买创建测试。
我不明白如何才能不伪造Google API函数querySkuDetailsAsync
的结果,该函数返回responseCode
和skuDetails
的列表,然后将其传递给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")
}