Android Espresso UI 测试 - 测试运行失败:仪器运行因“java.lang.IllegalAccessError”而失败

Posted

技术标签:

【中文标题】Android Espresso UI 测试 - 测试运行失败:仪器运行因“java.lang.IllegalAccessError”而失败【英文标题】:Android Espresso UI test - Test running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError' 【发布时间】:2015-10-16 11:59:26 【问题描述】:

我遇到了以下问题:

当我尝试使用 Espresso 启动独立 UI 测试时,我总是在没有运行测试的情况下收到以下控制台错误消息。

Test running startedTest running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'
Empty test suite.

所以我尝试运行命令:

 gradle -q dependencies

我得到了以下结果:

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.android.support:support-annotations:23.0.1
+--- com.android.support.test:runner:0.3
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.3
|    +--- junit:junit:4.12
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    \--- com.android.support:support-annotations:22.2.0 -> 23.0.1
+--- com.android.support.test:rules:0.3
|    \--- com.android.support.test:runner:0.3 (*)
+--- com.android.support.test.espresso:espresso-core:2.1 -> 2.2
|    +--- com.android.support.test.espresso:espresso-idling-resource:2.2
|    +--- com.squareup:javawriter:2.1.1
|    +--- javax.inject:javax.inject:1
|    +--- org.hamcrest:hamcrest-library:1.3
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    +--- com.android.support.test:rules:0.3 (*)
|    +--- org.hamcrest:hamcrest-integration:1.3
|    |    \--- org.hamcrest:hamcrest-library:1.3 (*)
|    +--- com.google.code.findbugs:jsr305:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- com.android.support.test:runner:0.3 (*)
+--- com.android.support.test.espresso:espresso-intents:2.2
|    \--- com.android.support.test.espresso:espresso-core:2.2 (*)
+--- com.android.support.test.espresso:espresso-web:2.2
|    +--- org.ccil.cowan.tagsoup:tagsoup:1.2
|    +--- com.android.support:support-annotations:22.2.0 -> 23.0.1
|    \--- com.android.support.test.espresso:espresso-core:2.2 (*)
+--- com.android.support.test.uiautomator:uiautomator-v18:2.1.1
+--- com.android.support.test.espresso:espresso-contrib:2.2
|    +--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.0
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    +--- com.android.support:support-v4:22.2.0
|    |    \--- com.android.support:support-annotations:22.2.0 -> 23.0.1
|    +--- com.android.support:recyclerview-v7:22.2.0
|    |    +--- com.android.support:support-annotations:22.2.0 -> 23.0.1
|    |    \--- com.android.support:support-v4:22.2.0 (*)
|    \--- com.android.support.test.espresso:espresso-core:2.2 (*)
\--- com.android.support:multidex-instrumentation:1.0.1

我尝试找到有关此主题的一些解决方案,但没有运气。

Tests fail after Espresso 2 upgrade (failed: Instrumentation run failed due to 'java.lang.IllegalAccessError')

我不知道我应该更新什么才能使测试正常工作。

这里是 app.gradle 配置文件

   android 
        compileSdkVersion 23
        buildToolsVersion "23.0.0"

        defaultConfig 
            applicationId "test.my.app"
            minSdkVersion 18
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            // Enabling multidex support.
            multiDexEnabled true
        
        buildTypes 
            release 
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            
        
        lintOptions 
            abortOnError false
        
        packagingOptions 
            exclude 'LICENSE.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
        
    


    repositories 
        jcenter()
        mavenCentral()
        maven  url "https://repo.commonsware.com.s3.amazonaws.com" 
        maven  url "https://oss.sonatype.org/content/repositories/snapshots/" 
        maven  url "https://jitpack.io" 
    

    dependencies 
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.google.android.gms:play-services:7.8.0'
        compile 'com.mcxiaoke.volley:library:1.0.18'
        compile 'com.orhanobut:logger:1.11'
        compile 'com.google.code.gson:gson:2.3.1'
        compile 'com.android.support:design:23.0.1'
        // UI TESTING DEPENDENCIES
        androidTestCompile 'com.android.support:support-annotations:23.0.1'
        androidTestCompile 'com.android.support.test:runner:0.3'
        androidTestCompile 'com.android.support.test:rules:0.3'
        //androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
        androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
        androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
        androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
        androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') 
            exclude group: 'com.android.support', module: 'multidex'
        
        // ORM DATABASE DEPENDENCIES
        compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
        // STETHO CORE
        compile 'com.facebook.stetho:stetho:1.1.1'
        compile 'com.facebook.stetho:stetho-urlconnection:1.1.1'
        compile 'com.facebook.stetho:stetho-okhttp:1.1.1'
        // GOOGLE ANALYTICS
        compile 'com.google.android.gms:play-services-analytics:7.8.0'
        // JODA TIME FOR ANDROID
        compile 'net.danlew:android.joda:2.8.2'
        // SECURE SHARED PREFERENCES
        compile 'com.scottyab:secure-preferences-lib:0.1.3'
        // MATERIAL DIALOG
        compile 'com.avast:android-styled-dialogs:2.2.0'
        // SQL CIPHER
        compile 'com.commonsware.cwac:sqlcipher-for-android:3.3.1'
        // html PARSER
        compile 'org.jsoup:jsoup:1.8.3'
        // MULTIDEX SUPPORT
        compile 'com.android.support:multidex:1.0.1'
        // SECOND MATERIAL DIALOG
        compile('com.afollestad.material-dialogs:core:0.8.1.0@aar') 
            transitive = true
        
        compile('com.afollestad.material-dialogs:commons:0.8.0.1@aar') 
            transitive = true
        
        // ANDROID BOOTSTRAP
        compile 'com.beardedhen:androidbootstrap:2.0.0'

    

以及 TEST 类的示例:

@RunWith(AndroidJUnit4.class)
public class SignInActivityTest extends ActivityInstrumentationTestCase2<SignInActivity> 

    public TestHelper tl = new TestHelper();
    public SignInActivityTest() 
        super(SignInActivity.class);
    

    @Before
    public void setUp() throws Exception 
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        SignInActivity lActivity = getActivity();
    

    // Insert scenario methods here
    @Test
    public void processTest() throws InterruptedException 

        // TODO: strings which are inserted into inputs should be defined as the string constants
        // Discuss it with Jan Sedlacek.
        tl.insertTextIntoInput(R.id.login_txv_username, "test");
        tl.insertTextIntoInput(R.id.login_txv_password, "test");
    


我应该改变什么才能使它正常工作?我会很高兴得到任何帮助。

【问题讨论】:

你解决了吗?我遇到了同样的问题。我的测试实际上是在扩展 AndroidTestCase。 我也遇到了同样的问题。有什么解决办法吗? 【参考方案1】:

ActivityInstrumentationTestCase2 已弃用,您必须改用ActivityTestRule

@RunWith(AndroidJUnit4.class)
public class SignInActivityTest 
    @Rule
    public ActivityTestRule<SignInActivity> mActivityRule = new ActivityTestRule(SignInActivity.class);

    public TestHelper tl = new TestHelper();

    @Before
    public void setUp() throws Exception 
        //currently you don't use Activity instance in your tests
        //I just put it here as an example how to get the activity from ActivityTestRule
        SignInActivity lActivity = mActivityRule.getActivity();
    

    // Insert scenario methods here
    @Test
    public void processTest() throws InterruptedException 
        tl.insertTextIntoInput(R.id.login_txv_username, "test");
        tl.insertTextIntoInput(R.id.login_txv_password, "test");
    

    @After
    public void tearDown() throws Exception 
        //do some stuff if needed
    

【讨论】:

天哪,这太疯狂了,这是我连续第二次在 2 天内偶然发现你的答案 xD【参考方案2】:

这是 MultiDex 的 Gradle 错误,如 Android issue 194609 中所述:

m...@felixschulze.de: ...似乎依赖项“multidex”已添加到应用程序本身和 androidTestCompile apk :(

见: https://android.googlesource.com/platform/tools/base/+/studio-1.5/build-system/gradle-core/src/main/groovy/com/android/build/gradle/internal/VariantManager.java#419

...

找到解决方法,将其添加到您的 build.gradle 文件中:

// Workaround for Multidex bug in gradle-android-plugin
// Replace Multidex dependency with some dummy dependency to avoid dex problems
// @see https://code.google.com/p/android/issues/detail?id=194609
project.getConfigurations().all  config ->
    if (config.name.contains("AndroidTest")) 
        config.resolutionStrategy.eachDependency  DependencyResolveDetails details ->
            if (details.requested.name == "multidex") 
                details.useTarget("de.felixschulze.teamcity:teamcity-status-message-helper:1.2")
            
        
    

而且,不要使用 MultiDexTestRunner。

【讨论】:

【参考方案3】:

我遇到了一个非常相似的问题。

我的问题是由某个工作线程中未捕获的InterruptedException 引起的,该线程停止了测试过程。堆栈跟踪显示在Android Monitor...我能够通过捕获异常来解决我的问题。

【讨论】:

【参考方案4】:

这意味着您在 Gradle 文件中存在问题。您使用了错误的依赖项。

我使用 robotsium 和 JUnit 4 我的依赖:

 dependencies 
     androidTestCompile fileTree(dir: 'libs', include: 'robotium-solo-5.5.4.jar')
     androidTestCompile 'junit:junit:4.12'
     androidTestCompile 'com.android.support.test:runner:0.4'
     // Set this dependency to use JUnit 4 rules
     androidTestCompile 'com.android.support.test:rules:0.4'
 

【讨论】:

以上是关于Android Espresso UI 测试 - 测试运行失败:仪器运行因“java.lang.IllegalAccessError”而失败的主要内容,如果未能解决你的问题,请参考以下文章

在Android Studio环境下使用ESPRESSO 测试框架进行UI测试

Android Espresso UI 测试 - 测试运行失败:仪器运行因“java.lang.IllegalAccessError”而失败

在ANDROID STUDIO环境下使用ESPRESSO 测试框架进行UI测试

Android测试:Espresso 自动化测试

在 Android Espresso UI 测试中从 Facebook 注销

回到地图意图espresso - UI测试的活动