找不到类“ android.support.test.runner.AndroidJUnitRunner”

时间:2018-10-09 08:56:28

标签: android

我正在尝试使用仪器化测试,并将项目更新为AndroidX。但是尝试运行时出现以下错误:

2018-10-09 14:22:29.468 13263-13263/xxx.yyy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx.yyy, PID: 13263
    java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{xxx.yyy.test/android.support.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5730)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5728)
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

我的测试如下:

package xxx.yyy;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("xxx.yyy", appContext.getPackageName());
    }
}

4 个答案:

答案 0 :(得分:13)

build.gradle文件中,您可以找到defaultConfig部分。使用testInstrumentationRunner属性,可以指定相应的测试运行程序。可以从android.support包中指定测试运行程序:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

这必须从androidx包更改为测试运行程序:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

答案 1 :(得分:8)

  

找不到类“ android.support .test.runner.AndroidJUnitRunner”

您迁移到AndroidX,但是您可以看到,项目中android.support测试库的仍然在使用中

要解决此问题,您应该在项目中以android.support开头的任何地方搜索任何内容,然后替换或删除它以使用AndroidX库。

答案 2 :(得分:1)

当我忘记为android测试运行器添加依赖项时,我遇到了同样的问题。
因此,添加此功能会有所帮助:

androidTestImplementation("androidx.test:runner:1.2.0")

答案 3 :(得分:0)

我失踪了

androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0

就我而言