AndroidJUnit4.class已被弃用:如何使用androidx.test.ext.junit.runners.AndroidJUnit4?

331

在我的仪器测试中,我正在使用

@RunWith(AndroidJUnit4.class)
import androidx.test.runner.AndroidJUnit4;

为了建立我的测试用例。现在这行代码被标记为过时,提示使用AndroidJUnit4

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

然而,如果我尝试从指定的包中导入AndroidJUnit4,我会收到错误信息,表示无法解析ext

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


https://dev59.com/S6Dia4cB1Zd3GeqPJc7_#43342884 - IntelliJ Amiya
4
我认为你的信息来源已过时,它们根本没有提到androidx。 - Marcel Gangwisch
2
Android上的单元测试是一个由错误和弃用API组成的混乱。唯一能够正常工作的方法是创建一个新的空白项目,将相关文件从该项目复制到我的项目中,切换到调试模式,并进行完全缓存失效。此外,在AndroidJUnit4测试中,软件包名称必须与库软件包名称匹配。 - Rupert Rawnsley
8个回答

475
根据AndroidJUnit4文档,需要遵循以下步骤:
  1. Gradle文件中应包含以下行:

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

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

如果仍然无法正常工作,请确保清理和/或重建项目。 您还可以直接在Google的Maven存储库中检查当前版本。


30
我的 Gradle 有这个,但仍无法解决问题。 - zuko
@JonAdams 我的依赖树因某种原因一直保留对旧库的引用,即使我认为我已经将它们删除了。所以在尝试了各种方法后,我最终只是将项目迁移到了一个新项目中,现在一切都很好。 - zuko
8
我们是否也应该将 Gradle 文件中的 _testInstrumentationRunner__androidx.test.runner.AndroidJUnitRunner_ 更改为 _androidx.test.ext.junit.runners.AndroidJUnit4_?只是想知道是否存在不匹配。 - Just The Highlights
25
请将导入旧的AndroidJUnit4包的代码改为导入新的AndroidJUnit4包: 将 import androidx.test.runner.AndroidJUnit4; 替换为 import androidx.test.ext.junit.runners.AndroidJUnit4; - Xplosive
AndroidJUnit4 的新包名是 import androidx.test.ext.junit.runners.AndroidJUnit4。如果您的 build.app 正确,那么它必须是一个不正确的包名。请注意,Android Studio 不会自动执行此操作。至少我使用的版本 Android Studio 3.3 在2018年12月25日更新。 - Muhammad Ahmed AbuTalib
显示剩余6条评论

51
如果你尝试过@MarcelGangwisch的解决方案,但是构建失败并且报错找不到资源,而且你已经清理/重建了项目还是不能正常工作,尝试以下方法: (也基于@KrzysztofDziuba的解决方案)
在你改变依赖的gradle文件中,请确保将它添加为你需要的类型,比如:
对于UI测试:
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
对于单元测试:
testImplementation 'androidx.test.ext:junit:1.1.0'
在我的情况下,我两个都添加了,现在它可以正常工作了。

26

对我而言,以下步骤起到了作用:
1. 使用此处发布的库替换androidx库。我的最终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();


4
“@RunWith(AndroidJUnit4ClassRunner.class)”是解决我问题的部分。我不太明白为什么Android Studio有时会生成过时的代码。 - Rafsanjani
1
请使用ApllicationProvider.getApplicationContext<Context>()代替InstrumentationRegistry.getInstrumentation().getTargetContext(); - Mitch

21
解决方案
  1. 在 build.gradle 中添加此行:androidTestImplementation 'androidx.test.ext:junit:1.1.1'

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

警告

我遇到了同样的错误,但以下解决方案对我无效:

文件 -> 清除缓存 ..., 选择清除并重启


10

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


7
  1. 在build.gradle的应用程序模块中包含以下行。

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

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

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

  3. 如果InstrumentationRegistry.getTargetContext()已被弃用,请使用androidx.test.platform.app中的InstrumentationRegistry,像这样:

    InstrumentationRegistry.getInstrumentation().getTargetContext()


6

3
如果您已经导入了这些AndroidX测试库,同步并重新构建了项目,但是导入的包仍未解决。只需记住如何将项目升级到AndroidX,关闭Android Studio并删除.idea文件夹,然后再次打开您的项目......

这对我起作用了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接