如何使用Mockk模拟Build.VERSION.SDK_INT

时间:2019-01-24 23:11:49

标签: android testing kotlin android-testing mockk

如何在模拟中模拟Build.VERSION.SDK_INT?

我已完成以下操作:

@Test
fun testFoo(){
    mockkStatic(Build::class)
    mockkStatic(Build.VERSION::class)
    every {
        Build.VERSION.SDK_INT
    } answers { 22 }
}

一旦代码触及io.mockk.MockKException: Missing calls inside every { ... } block.块,我最终会得到every

1 个答案:

答案 0 :(得分:0)

您可以像这样围绕Build配置构建包装器

object MyAppBuildConfig {
    fun getVersionSDKInt(): Int {
        return Build.VERSION.SDK_INT
    }
}

然后使用模拟对象模拟MyAppBuildConfig并返回所需的版本号

    mockkObject(MyAppBuildConfig)
    every { MyAppBuildConfig.getVersionSDKInt() } returns 22