Application.class mock in Android UnitTest

huangapple go评论84阅读模式
英文:

Application.class mock in Android UnitTest

问题

我有一个Application类。

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类。

我需要在UnitTest中使用它,而不是androidTest。

在UNITTEST中如何创建或模拟Application?

英文:

I have an Application class.

open class AppController : MultiDexApplication() {

    companion object {

        @JvmStatic
        lateinit var instance: AppController
            private set

    }

    override fun onCreate() {
        super.onCreate()

        instance = this
    }

}

I use my code for extension.
Int.kt

fun Int.pxToDp(): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), AppControllerMaster.instance.applicationContext.resources.displayMetrics).toInt()
}

I need to use that in a unit test.
when use that I get this error

kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized

I need to create a mock or alternative AppController.class in my unit test.

I need to use it in UnitTest, not androidTest.

how can Application Crete or mock in UNITTEST?

答案1

得分: 1

参考http://robolectric.org/

Robolectric是一个为Android带来快速可靠的单元测试的框架。
测试在您的工作站上在JVM内部在几秒钟内运行。
英文:

Refer http://robolectric.org/

Robolectric is a framework that brings fast and reliable unit tests to Android.
Tests run inside the JVM on your workstation in seconds

答案2

得分: 1

我在不使用Robolectric的情况下找到了一个答案

我在名为ContextEX.kt的类中的com.example包中创建了一个函数扩展

**ContextEX.kt**

```kotlin
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库模拟了Application类和上下文扩展:

val context: Context = spyk()

// Mock上下文扩展
mockkStatic("com.example.ContextEXKt") // 注意:不是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
}
英文:

I found an answer without using Robolectric.

I create a function extension in package com.example in class with the name ContextEX.kt

ContextEX.kt

fun Any.getContextEX(): Context {
    return AppController.instance
}

and change pxToDp extention.

change AppControllerMaster.instance.applicationContext to getContextEX().

fun Int.pxToDp(): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), getContextEX().resources.displayMetrics).toInt()
}

and in the test, I mock Application class and context extension with Mockk library

val context: Context = spyk()

// Mock Context extension
mockkStatic(&quot;com.example.ContextEXKt&quot;) // notic: not ContextEX.kt

val metrics: DisplayMetrics = mockk()
val resources: Resources = mockk()

every {
   any&lt;Any&gt;().getContext()
}.answers {
   context
}

every {
   any&lt;Any&gt;().getContext().resources
}.answers {
   resources
}

every {
   any&lt;Any&gt;().getContext().resources.displayMetrics
}.answers {
   metrics
}

huangapple
  • 本文由 发表于 2020年10月20日 22:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64447822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定