本文介绍了Android Studio 找不到 AndroidJUnit4.class + 测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试开始这样的测试时:

When I try to start a test like this:

./gradlew app:connectedCheck

那么成绩输出将是:

deleteDir(/myapp/build/outputs/androidTest-results/connected) returned: true
deleteDir(/myapp/build/outputs/code-coverage/connected) returned: true
Starting 0 tests on Device - 4.4.2
Tests on Device - 4.4.2 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

com.android.builder.testing.ConnectedDevice > No tests found.[Device - 4.4.2] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
deleteDir(/myapp/build/reports/androidTests/connected) returned: true
:app:connectedAndroidTestDebug FAILED
:app:connectedAndroidTestDebug (Thread[Task worker Thread 3,5,main]) completed. Took 16.012 secs.

这是我的 build.gradle:

Heres my build.gradle:

androidTestCompile "junit:junit:${JUNIT_VERSION}"
androidTestCompile "org.objenesis:objenesis:${OBJENESIS_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-core:${HAMCREST_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-integration:${HAMCREST_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-library:${HAMCREST_VERSION}"
androidTestCompile "com.android.support:support-v4:${SUPPORT_LIBRARY_VERSION}"
androidTestCompile "com.android.support:recyclerview-v7:${SUPPORT_LIBRARY_VERSION}"
androidTestCompile "org.mockito:mockito-core:${MOCKITO_VERSION}"
androidTestCompile "com.google.dexmaker:dexmaker:${DEXMAKER_VERSION}"
androidTestCompile("com.google.dexmaker:dexmaker-mockito:${DEXMAKER_VERSION}") {
    exclude group: 'org.mockito', module: 'mockito-core'
}

// Android test
androidTestCompile "com.android.support.test:runner:${ANDROID_TEST_VERSION}"
androidTestCompile "com.android.support.test:rules:${ANDROID_TEST_VERSION}"
androidTestCompile "com.android.support.test.uiautomator:uiautomator-v18:${UIAUTOMATOR_VERSION}"

// Espresso dependencies
androidTestCompile "com.android.support.test.espresso:espresso-core:${ESPRESSO_VERSION}"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:${ESPRESSO_VERSION}"
androidTestCompile "com.android.support.test.espresso:espresso-intents:${ESPRESSO_VERSION}"

以及 gradle.properties 的相关部分:

And relevant part of gradle.properties:

# Testing dependencies.
ANDROID_TEST_VERSION = 0.2
# TODO: Upgrade, as soon as https://github.com/crittercism/dexmaker/issues/16 is pulled
DEXMAKER_VERSION = 1.2
ESPRESSO_VERSION = 2.1
HAMCREST_VERSION = 1.3
# Do not use JUnit 4.12, until Powermock can be upgraded to >1.6
# See http://stackoverflow.com/a/26222732/375209 for details.
JUNIT_VERSION = 4.11
# Raising Mockito version will java.lang.AbstractMethodError: abstract method not implemented until dexmaker can be
# upgraded to 1.3 (https://github.com/crittercism/dexmaker/pull/14 & https://github.com/crittercism/dexmaker/issues/16)
# the new version.
# MOCKITO_VERSION = 1.10.19
MOCKITO_VERSION = 1.9.5
OBJENESIS_VERSION = 1.3
# This must be in sync with the Mockito version, as Mockito X only works with Powermock Y.
# See this file: https://code.google.com/p/powermock/wiki/MockitoUsage13
POWERMOCK_VERSION = 1.5.6
ROBOLECTRIC_VERSION = 2.4
UIAUTOMATOR_VERSION = 2.1.0

Android Studio 也失败,因为它无法解析 JUnit 运行程序.

Android Studio fails as well because it can not resolve the JUnit runner.

让事情正常工作的缺失开关在哪里?

Where is the missing switch to get things working?

推荐答案

现在 dexmaker 1.3 和 android test support runner 0.3 已经发布,这开始工作了:

Now that dexmaker 1.3 and android test support runner 0.3 are released, this started working:

androidTestCompile "junit:junit:${JUNIT_VERSION}"
androidTestCompile "com.android.support:support-v4:${SUPPORT_V4_VERSION}"
androidTestCompile "com.android.support:recyclerview-v7:${SUPPORT_LIBRARY_VERSION}"
androidTestCompile "org.mockito:mockito-core:${MOCKITO_VERSION}"
androidTestCompile "com.crittercism.dexmaker:dexmaker:${DEXMAKER_VERSION}"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:${DEXMAKER_VERSION}"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:${DEXMAKER_VERSION}"

// Android test
androidTestCompile "com.android.support.test:runner:${ANDROID_TEST_VERSION}"
androidTestCompile "com.android.support.test:rules:${ANDROID_TEST_VERSION}"
androidTestCompile "com.android.support.test.uiautomator:uiautomator-v18:${UIAUTOMATOR_VERSION}"

// Espresso dependencies
androidTestCompile "com.android.support.test.espresso:espresso-core:${ESPRESSO_VERSION}"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:${ESPRESSO_VERSION}"
androidTestCompile "com.android.support.test.espresso:espresso-intents:${ESPRESSO_VERSION}"

gradle.properties:

gradle.properties:

# Testing dependencies.
ANDROID_TEST_VERSION = 0.3
DEXMAKER_VERSION = 1.3
ESPRESSO_VERSION = 2.2
JUNIT_VERSION = 4.12
MOCKITO_VERSION = 1.10.19
POWERMOCK_VERSION = 1.6.2
ROBOLECTRIC_VERSION = 2.4
UIAUTOMATOR_VERSION = 2.1.1

这篇关于Android Studio 找不到 AndroidJUnit4.class + 测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:03