我正在尝试使用仪器化测试,并将项目更新为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());
}
}
答案 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
就我而言