【问题标题】:AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?AndroidJUnit4.class 已弃用:如何使用 androidx.test.ext.junit.runners.AndroidJUnit4?
【发布时间】:2019-03-17 12:27:39
【问题描述】:

对于我正在使用的仪器测试

@RunWith(AndroidJUnit4.class)

来自

import androidx.test.runner.AndroidJUnit4;

为了建立我的测试用例。现在这条线被标记为已弃用,并提示使用AndroidJUnit4 from

import androidx.test.ext.junit.runners.AndroidJUnit4

但是,如果我尝试从命名包中导入 AndroidJUnit4,则会收到错误,即无法解析 ext

你有什么想法,应该在 gradle 中包含什么包来解决这个问题?

【问题讨论】:

  • 我认为你的来源已经过时了,他们根本没有提到 androidx。
  • Android 上的单元测试完全是一堆错误和弃用。唯一对我有用的是创建一个新的临时项目,将相关文件从该项目复制到我的项目中,切换到调试模式,并进行完全缓存失效。此外,对于 AndroidJUnit4 测试,包名必须与库包名匹配。

标签: android junit android-testing androidx


【解决方案1】:

根据the documentation for AndroidJUnit4

  1. gradle 文件应包含以下行:

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

  1. 将测试类从 AndroidJUnit4 更改为 AndroidJUnit4ClassRunner

如果仍然无法正常工作,请确保您清理和/或重建您的项目。也可以直接在Google's maven repository查看当前版本

【讨论】:

  • 我的 gradle 有这个问题,但仍然无法解决问题。
  • @zuko 清理和/或重建您的项目,它应该在那里。如果仍然没有,并且您正在使用模块,请检查您的依赖层次结构是否按预期设置;您可能还需要将这些添加到顶级模块中——因为测试库没有编译成共享模块供上游模块使用。
  • @JonAdams 我的依赖树出于某种原因一直保留对旧库的引用,即使我认为我删除了它们。所以在尝试了各种事情之后,我最终只是将项目迁移到一个新项目,现在一切都很好。
  • 我们是否也应该将 gradle 文件中的 testInstrumentationRunnerandroidx.test.runner.AndroidJUnitRunner 更改为 androidx.test.ext.junit .runners.AndroidJUnit4?只是想知道是否有不匹配。
  • @A Droid 将导入时的旧 AndroidJUnit4 包替换为新包。只需替换 import androidx.test.runner.AndroidJUnit4; with import androidx.test.ext.junit.runners.AndroidJUnit4;
【解决方案2】:

在我的情况下,将 androidTestImplementation 更改为 testImplementation 有帮助。 在阅读本文之前我不知道有什么区别android difference between testImplementation and androidTestImplementation in build.gradle

【讨论】:

    【解决方案3】:

    如果您尝试了@MarcelGangwisch 的解决方案,但您的构建失败并说找不到资源,并且您还清理/重建了您的项目,但它仍然无法正常工作,请尝试以下操作: (也基于@KrzysztofDziuba 的解决方案)

    在您更改依赖项的 gradle 文件中,确保将其添加为您需要的类型,即:

    对于 UI 测试:

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

    对于单元测试:

    testImplementation 'androidx.test.ext:junit:1.1.0'

    在我的例子中,我将它添加为两者,现在它可以工作了。

    【讨论】:

      【解决方案4】:

      我尝试了以上所有方法,直到我访问官方 Android 网站,他们建议从 androidx.test.ext.junit.runners.AndroidJUnit4 导入而不是 androidx.test.runner.AndroidJUnit4link

      【讨论】:

        【解决方案5】:

        对我来说,以下步骤有效:
        1. 将 androidx 库替换为 here 发布的库。我最后的app/build.gradle 看起来像这样:

        android {
            ...
            defaultConfig {
                ...
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        
            }
            ...
        }
        
        dependencies {
            ...
            testImplementation 'junit:junit:4.12'
        
            // Core library
            androidTestImplementation 'androidx.test:core:1.2.0'
        
            // AndroidJUnitRunner and JUnit Rules
            androidTestImplementation 'androidx.test:runner:1.2.0'
            androidTestImplementation 'androidx.test:rules:1.2.0'
        
            // Assertions
            androidTestImplementation 'androidx.test.ext:junit:1.1.1'
            androidTestImplementation 'androidx.test.ext:truth:1.2.0'
            androidTestImplementation 'com.google.truth:truth:0.42'
        
            // Espresso dependencies
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        
        }
        

        然后我手动将 ExampleInstrumentTest.java 类中的导入模块替换为最新的类:

        import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
        import androidx.test.platform.app.InstrumentationRegistry;
        import androidx.test.rule.ActivityTestRule;
        import org.junit.Assert;
        import org.junit.Before;
        import org.junit.Rule;
        import org.junit.Test;
        import org.junit.runner.RunWith;
        
        @RunWith(AndroidJUnit4ClassRunner.class)
        public class ExampleInstrumentedTest {
            ...
            @Rule
            public final ActivityTestRule<MainActivity> main = new ActivityTestRule<>(MainActivity.class, true);
        
            @Before
            public void init() {
                ...
            }
            @Test
            public void listCount() {
                ...
            }
        
            @Test
            public void useAppContext() {
                Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        
                Assert.assertEquals("in.curioustools.aad_x_testing2", appContext.getPackageName());
                System.out.println("useAppContext : Test Ran");
            }
        }
        

        困扰我的是InstrumentationRegistery 类仍然被弃用的事实。所以我使用了androidx.test.platform.app.InstrumentationRegistry类中的InstrumentationRegistry.getInstrumentation().getTargetContext();

        【讨论】:

        • @RunWith(AndroidJUnit4ClassRunner.class) 是在我的案例中解决问题的部分。我真的不明白为什么 android studio 有时会生成不推荐使用的代码。
        • 使用ApllicationProvider.getApplicationContext&lt;Context&gt;() 而不是InstrumentationRegistry.getInstrumentation().getTargetContext();
        【解决方案6】:

        如果你导入了那些 AnroidX 测试库,同步并重新构建了项目,但导入的包仍然没有解析。请记住您如何将项目升级到 AndroidX,关闭 Android Studio 并删除 .idea 文件夹并再次重新打开您的项目...

        这对我有用!

        【讨论】:

          【解决方案7】:

          解决方案

          1. 将此行添加到 build.gradle:androidTestImplementation 'androidx.test.ext:junit:1.1.1'

          2. 在测试类中将AndroidJUnit4改为AndroidJUnit4ClassRunner

          警告

          我遇到了同样的错误,但以下解决方案对我来说失败了:

          File -> Invalidate Caches...,然后选择 Invalidate and Restart

          【讨论】:

            【解决方案8】:
            1. 在 build.gradle app 模块中包含这些行。

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

            2. 在测试类中将你的测试运行器替换为

              导入androidx.test.ext.junit.runners.AndroidJUnit4;

            3. 如果不推荐使用 InstrumentationRegistry.getTargetContext(),请使用 InstrumentationRegistry from androidx.test.platform.app 像这样

              InstrumentationRegistry.getInstrumentation().getTargetContext()

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-05-18
              • 1970-01-01
              • 2014-02-12
              • 2023-02-19
              • 2013-11-14
              • 2010-12-04
              相关资源
              最近更新 更多