除非应用程序中包含测试依赖,否则安卓测试会失败

0 人关注

我们有在模拟器中(或在真实设备上)运行的Android测试。

最近,这些测试开始无法运行,出现以下错误。

java.lang.NoSuchMethodError: No virtual method getDeclaringClass()Ljava/lang/Class; in class Lorg/junit/runners/model/FrameworkMethod; or its super classes (declaration of 'org.junit.runners.model.FrameworkMethod' appears in /data/app/com.example.myapp-AYAInqNVkGzlPMGy6LaKSw==/base.apk!classes19.dex)
at androidx.test.internal.runner.junit4.statement.UiThreadStatement.classHasAnnotation(UiThreadStatement.java:98)
at androidx.test.internal.runner.junit4.statement.UiThreadStatement.hasAnnotation(UiThreadStatement.java:93)
at androidx.test.internal.runner.junit4.statement.UiThreadStatement.shouldRunOnUiThread(UiThreadStatement.java:74)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.methodInvoker(AndroidJUnit4ClassRunner.java:61)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:251)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:162)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:444)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
01-27 11:35:48.687 10255 10303 E TestRunner: failed: loginTest(com.example.myapp.activities.uitests.LoginTest)
01-27 11:35:48.688 10255 10303 E TestRunner: ----- begin exception -----
01-27 11:35:48.698 10255 10303 E TestRunner: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/hamcrest/Matchers;
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.espresso.matcher.ViewMatchers.withId(ViewMatchers.java:1)
01-27 11:35:48.698 10255 10303 E TestRunner:    at com.example.myapp.activities.TestingPermissions.loginMethod(TestingPermisions.kt:75)
01-27 11:35:48.698 10255 10303 E TestRunner:    at com.example.myapp.activities.uitests.LoginTest.loginTest(LoginTest.kt:37)
01-27 11:35:48.698 10255 10303 E TestRunner:    at java.lang.reflect.Method.invoke(Native Method)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:549)
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:134)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:162)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.Suite.runChild(Suite.java:128)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.Suite.runChild(Suite.java:27)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
01-27 11:35:48.698 10255 10303 E TestRunner:    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
01-27 11:35:48.698 10255 10303 E TestRunner:    at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:444)
01-27 11:35:48.698 10255 10303 E TestRunner:    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
01-27 11:35:48.698 10255 10303 E TestRunner: Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.Matchers" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.example.myapp.test-O96KLGwojXcLFHg_hVK1-A==/base.apk", zip file "/data/app/com.example.myapp-rGS0dWVmuKcmSSbiwfPtPA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapp.test-O96KLGwojXcLFHg_hVK1-A==/lib/x86, /data/app/com.example.myapp-rGS0dWVmuKcmSSbiwfPtPA==/lib/x86, /data/app/com.example.myapp.test-O96KLGwojXcLFHg_hVK1-A==/base.apk!/lib/x86, /data/app/com.example.myapp-rGS0dWVmuKcmSSbiwfPtPA==/base.apk!/lib/x86, /system/lib]]
01-27 11:35:48.698 10255 10303 E TestRunner:    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
01-27 11:35:48.698 10255 10303 E TestRunner:    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
01-27 11:35:48.698 10255 10303 E TestRunner:    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
01-27 11:35:48.698 10255 10303 E TestRunner:    ... 34 more
01-27 11:35:48.698 10255 10303 E TestRunner: ----- end exception -----

回溯git历史,我发现这些错误是从build.gradle中的这些行被修改后开始的。

Before

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation "androidx.test:core:1.4.0"
    androidTestImplementation "androidx.test:core-ktx:1.4.0"
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    implementation 'androidx.test.espresso:espresso-contrib:3.4.0'
    implementation 'androidx.test.ext:junit-ktx:1.1.3'

After

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation "androidx.test:core:1.4.0"
    androidTestImplementation "androidx.test:core-ktx:1.4.0"
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'