Application.class 在 Android 单元测试中模拟
Application.class mock in Android UnitTest
我有一个应用程序 class。
open class AppController : MultiDexApplication() {
companion object {
@JvmStatic
lateinit var instance: AppController
private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
我使用我的代码进行扩展。
Int.kt
fun Int.pxToDp(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), AppControllerMaster.instance.applicationContext.resources.displayMetrics).toInt()
}
我需要在单元测试中使用它。
使用时出现此错误
kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized
我需要在我的单元测试中创建模拟或替代 AppController.class。
我需要在 UnitTest 中使用它,而不是 androidTest。
如何在 UNITTEST 中应用 Crete 或 mock?
Robolectric is a framework that brings fast and reliable unit tests to Android.
Tests run inside the JVM on your workstation in seconds
我在没有使用 Robolectric 的情况下找到了答案。
我在 class 中的包 com.example 中创建了一个名为 ContextEX.kt
的函数扩展
ContextEX.kt
fun Any.getContextEX(): Context {
return AppController.instance
}
并更改 pxToDp 扩展。
将 AppControllerMaster.instance.applicationContext
更改为 getContextEX()
。
fun Int.pxToDp(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), getContextEX().resources.displayMetrics).toInt()
}
并且在测试中,我使用 Mockk 库模拟应用程序 class 和上下文扩展
val context: Context = spyk()
// Mock Context extension
mockkStatic("com.example.ContextEXKt") // notic: not ContextEX.kt
val metrics: DisplayMetrics = mockk()
val resources: Resources = mockk()
every {
any<Any>().getContext()
}.answers {
context
}
every {
any<Any>().getContext().resources
}.answers {
resources
}
every {
any<Any>().getContext().resources.displayMetrics
}.answers {
metrics
}
我有一个应用程序 class。
open class AppController : MultiDexApplication() {
companion object {
@JvmStatic
lateinit var instance: AppController
private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
我使用我的代码进行扩展。 Int.kt
fun Int.pxToDp(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), AppControllerMaster.instance.applicationContext.resources.displayMetrics).toInt()
}
我需要在单元测试中使用它。 使用时出现此错误
kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized
我需要在我的单元测试中创建模拟或替代 AppController.class。
我需要在 UnitTest 中使用它,而不是 androidTest。
如何在 UNITTEST 中应用 Crete 或 mock?
Robolectric is a framework that brings fast and reliable unit tests to Android.
Tests run inside the JVM on your workstation in seconds
我在没有使用 Robolectric 的情况下找到了答案。
我在 class 中的包 com.example 中创建了一个名为 ContextEX.kt
的函数扩展ContextEX.kt
fun Any.getContextEX(): Context {
return AppController.instance
}
并更改 pxToDp 扩展。
将 AppControllerMaster.instance.applicationContext
更改为 getContextEX()
。
fun Int.pxToDp(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), getContextEX().resources.displayMetrics).toInt()
}
并且在测试中,我使用 Mockk 库模拟应用程序 class 和上下文扩展
val context: Context = spyk()
// Mock Context extension
mockkStatic("com.example.ContextEXKt") // notic: not ContextEX.kt
val metrics: DisplayMetrics = mockk()
val resources: Resources = mockk()
every {
any<Any>().getContext()
}.answers {
context
}
every {
any<Any>().getContext().resources
}.answers {
resources
}
every {
any<Any>().getContext().resources.displayMetrics
}.answers {
metrics
}