单元测试 AndroidViewModel 类 [英] Unit Testing AndroidViewModel classes

查看:43
本文介绍了单元测试 AndroidViewModel 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用编写单元测试,我在编写它们时发现了一个减速带".在测试 AndroidViewModel 的子类时,我缺少用于初始化的 Application 参数.我已经阅读了这个使用 Robolectric 的问题.

Im writting Unit Tests for my app and I've found a "speed bump" while writting them. While testing subclasses of AndroidViewModel im missing the Application parameter for its initialization. I've already read this question that uses Robolectric.

这是我迄今为止已经尝试过的:

This is what I already tried so far:

  • 按照问题描述使用 Robolectric.据我了解,Robolectric 可以使用您的自定义应用程序类进行测试,问题是我没有使用自定义应用程序类,因为我不需要它.(应用程序没那么复杂).
  • 使用模拟.Mockito 抛出一个异常,指出不能模拟 Context 类.
  • 使用 InstrumentationRegistry.我将测试类从 test 文件夹移动到 androidTest 文件夹,让我可以访问 androidTestImplementation 依赖项,我尝试使用 InstrumentationRegistry.getContext() 并将其解析为 Application,当然这没有工作,抛出一个强制转换异常.尝试这个我觉得很愚蠢,但同样值得一试.
  • Using Robolectric as the question describes. As far as I understand, Robolectric can use your custom Application class for testing, the thing is im not using a custom application class as I dont need it. (the app is not that complex).
  • Using mockito. Mockito throws an exception saying that the Context class cannot be mocked.
  • Using the InstrumentationRegistry. I moved the test classes from the test folder to the androidTest folder, giving me access to the androidTestImplementation dependencies, I tried using the InstrumentationRegistry.getContext() and parse it to Application, of course this didn't worked, throwing a cast exception. I felt so dumb trying this but again, it was worth the shot.

我只想实例化我的 AndroidViewModel 类,以便我可以调用它们的公共方法,但需要 Application 参数.我能为此做些什么?

I just want to instanciate my AndroidViewModel classes so I can call their public methods, but the Application parameter is needed. What can I do for this?

fun someTest() {
   val testViewModel = MyViewModelThatExtendsFromAndroidViewModel(**missing application parameter**)
   testViewModel.foo() // The code never reaches here as the testViewModel cant be initializated
}

推荐答案

我遇到了同样的问题,找到了两个解决方案.

I had the same issue, and found two solutions.

您可以在单元测试中的测试目录中使用 Robolectric,然后选择平台应用程序 类.

You can use Robolectric in a Unit Test, inside test directory, and choose the platform Application class.

@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class ViewModelTest {

    @Test
    @Throws(Exception::class)
    fun someTest() {
        val application = RuntimeEnvironment.application
        val testViewModel = MyViewModelThatExtendsFromAndroidViewModel(application)
        testViewModel.foo()
    }

}

或者您可以在 androidTest 目录中使用 InstrumentationTest,并将 InstrumentationRegistry.getTargetContext().applicationContext 转换为 Application:

Or you can use an InstrumentationTest inside androidTest directory, and cast the InstrumentationRegistry.getTargetContext().applicationContext to Application:

@RunWith(AndroidJUnit4::class)
class ViewModelTest {

    @Test
    @Throws(Exception::class)
    fun someTest() {
        val application = ApplicationProvider.getApplicationContext() as Application
        val testViewModel = MyViewModelThatExtendsFromAndroidViewModel(application)
        testViewModel.foo()
    }

}

希望有帮助!

这篇关于单元测试 AndroidViewModel 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆