【问题标题】:After AndroidX migration AndroidTestCase, AndroidJUnit4 & InstrumentationRegistry.getTargetContext are depricatedAndroidX 迁移 AndroidTestCase 后,AndroidJUnit4 & InstrumentationRegistry.getTargetContext 被弃用
【发布时间】:2019-07-09 20:17:25
【问题描述】:

在 AndroidX 迁移之后,我所有扩展 AndroidTestCase 的 Android 测试都停止工作。 我决定改用@RunWith(AndroidJUnit4.class),但这也已弃用,甚至InstrumentationRegistry.getTargetContext() 也已弃用。 我想知道用不推荐的东西更新我的测试的正确方法。

【问题讨论】:

    标签: java android kotlin androidx


    【解决方案1】:

    我所有扩展 AndroidTestCase 的 Android 测试都停止工作

    随着 Android 7.0 的发布,AndroidTestCase 已弃用 three years ago

    我已决定改用 @RunWith(AndroidJUnit4.class) 但这也已弃用

    使用androidx.test.ext.junit.runners.AndroidJUnit4

    甚至 InstrumentationRegistry.getTargetContext() 也被弃用了

    如果您使用的是androidx.test.platform.app.InstrumentationRegistry,请致电getInstrumentation().getTargetContext()

    【讨论】:

    • 谢谢,它成功了。此外,我必须在我的 build.gradle 中使用 androidTestImplementation "androidx.test.ext:junit:1.1.1" 才能使 androidx.test.ext.junit.runners.AndroidJUnit4 工作
    • 那么哪个Context返回ApplicationProvider?
    • @UserOne:对不起,我不明白你的问题。
    • 基于这个《getInstrumentation().getTargetContext().》 ApplicationsProvider也返回了Test下的App Context
    • @UserOne:啊,我明白了。是的,ApplicationProvider has a static getApplicationContext() method 可用于获取待测代码的 Application 单例。我更专注于修改问题中的代码。
    【解决方案2】:

    将此行添加到您的 build.gradle:

    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    

    https://developer.android.com/reference/androidx/test/runner/AndroidJUnit4

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多