不推荐使用AndroidJUnit4.class:如何使用androidx.test.ext.junit.runners.AndroidJUnit4?

时间:2018-10-12 09:40:59

标签: android junit android-testing androidx

我使用的仪器测试

@RunWith(AndroidJUnit4.class)

来自

import androidx.test.runner.AndroidJUnit4;

以建立我的测试用例。现在,此行被标记为已弃用,并提示使用AndroidJUnit4来自

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

但是,如果我尝试从命名包中导入AndroidJUnit4,则会收到错误消息,提示ext无法解决。

您有什么主意,应在gradle中包括哪些软件包来解决此问题?

8 个答案:

答案 0 :(得分:149)

根据the documentation for AndroidJUnit4,gradle文件应包含以下行:

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

添加完后,一切对我都有效。

如果仍然无法正常工作,请确保清理和/或重建项目。

答案 1 :(得分:5)

对我来说,以下步骤有效:
1.用发布的here替换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类仍被弃用的事实。所以我使用了InstrumentationRegistry.getInstrumentation().getTargetContext();类的androidx.test.platform.app.InstrumentationRegistry

答案 2 :(得分:4)

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

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

对于UI测试:

  

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

对于单元测试:

  

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

在我的实例中,我将两者都添加了,现在可以使用了。

答案 3 :(得分:4)

在尝试将项目添加到github后,在ExampleInstrumentedTest.java文件中遇到了相同的错误。 此选项对我不起作用 File-> Invalidate Caches ...,然后选择Invalidate and Restart

然后我这样做。...这对我有用

将此行添加到build.gradle

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

答案 4 :(得分:4)

  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(),请像这样使用InstrumentationRegistry中的androidx.test.platform.app

    InstrumentationRegistry.getInstrumentation()。getTargetContext()

答案 5 :(得分:1)

就我而言,将androidTestImplementation更改为testImplementation很有帮助。 在阅读本android difference between testImplementation and androidTestImplementation in build.gradle

之前,我不知道有什么区别

答案 6 :(得分:1)

我尝试了上面给出的所有操作,直到访问了Android官方网站,他们建议从androidx.test.ext.junit.runners.AndroidJUnit4而不是androidx.test.runner.AndroidJUnit4进行导入。 link

答案 7 :(得分:0)

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

这对我有用!