【问题标题】:Android Espresso test fails with IncompatibleClassChangeErrorAndroid Espresso 测试失败并出现 IncompatibleClassChangeError
【发布时间】:2016-03-10 07:27:52
【问题描述】:

我正在尝试学习适用于 android 的 Espresso。为此,我创建了一个简单的 android studio 项目。但是我在运行测试用例时遇到了一些问题。

这是我的模块级别build.gradle 文件:

应用插件:'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.abdullah.mytestapplication"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.2.0'

    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support:support-annotations:23.2.0'

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}

我的测试用例是:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mainActivityRule =
            new ActivityTestRule<>(MainActivity.class);

    @Test
    public void myTest() {
        Assert.assertNotNull(new Object());
    }
}

测试运行的输出是:

Running tests
Test running started
java.lang.IncompatibleClassChangeError: com.abdullah.mytestapplication.MainActivity
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:338)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.abdullah.mytestapplication.MainActivityTest.<init>(MainActivityTest.java:19)
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$

----------

.run(Instrumentation.java:1970)

Finish

我不确定这个异常的原因是什么以及如何摆脱它。当我在设备或模拟器上运行时,行为是相同的。

【问题讨论】:

    标签: android unit-testing android-espresso android-instrumentation


    【解决方案1】:

    我从this 链接中找到了解决方法。

    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude module: 'support-v4'
    }
    

    排除 support-v4 frm expresso-contrib 使其工作。


    编辑

    对于版本2.2.2需要做如下:

    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude module: 'support-annotations'
        exclude module: 'support-v4'
        exclude module: 'design'
        exclude module: 'recyclerview-v7'
    }
    

    【讨论】:

    • 嗯,在为 espresso-contrib 添加排除行后,我仍然遇到同样的错误。我正在使用 espresso-contribL:2.2.2。知道为什么吗?
    • @clu 您需要添加以下exclude module: 'support-annotations' exclude module: 'support-v4' exclude module: 'design' exclude module: 'recyclerview-v7'
    • @Abdullah 这个答案并没有解决我这边的问题。
    • @X-HuMan 我也没有
    猜你喜欢
    • 2022-06-14
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多