1

当我添加

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

在 gradle 中并运行 Espresso 测试,我收到以下错误:

 Testing started at 5:59 PM ...

02/08 17:59:58: Launching changeText_sameAct...()
No apk changes detected since last installation, skipping installation of /Users/xxx/Documents/home/MasterDetails/app/build/outputs/apk/app-debug.apk
$ adb shell am force-stop databinding.com.masterdetails
No apk changes detected since last installation, skipping installation of /Users/xxx/Documents/home/MasterDetails/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
$ adb shell am force-stop databinding.com.masterdetails.test
Running tests

$ adb shell am instrument -w -r   -e debug false -e class databinding.com.masterdetails.TestPostListActivity#changeText_sameActivity databinding.com.masterdetails.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..Waiting for process to come online
Connected to process 10355 on device samsung-sm_g920f-01157df1bc4b5336
Test running started

java.lang.IncompatibleClassChangeError: databinding.com.masterdetails.PostListActivity
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:321)
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 databinding.com.masterdetails.TestPostListActivity.<init>(TestPostListActivity.java:25)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
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$InstrumentationThread.run(Instrumentation.java:1889)

Tests ran to completion.

并且测试没有运行。我怀疑是 gradle 问题所以这是我的应用程序 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId "databinding.com.masterdetails"
        minSdkVersion 18
        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'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'


    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    // Android JUnit Runner
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    // JUnit4 Rules
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Espresso core
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.1'


}

我需要添加

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

所以我可以用 RecyclerView 进行测试。

4

2 回答 2

1

请检查我的依赖项,尤其是espresso那些。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    androidTestCompile "com.android.support:support-annotations:$ASVersion"
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile "com.android.support:appcompat-v7:$ASVersion"
    compile "com.android.support:support-v4:$ASVersion"
    compile "com.android.support:design:$ASVersion"

}

在哪里def ASVersion = '23.1.1'

也许你错过了什么。我的已经工作了

希望有帮助

于 2016-02-20T20:20:59.713 回答
0

似乎您包含RecyclerView两次组件: here -compile 'com.android.support:recyclerview-v7:23.1.1'和 here androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'。在 after dependencies 部分添加以下代码,build.gradle然后重试:

configurations {
    androidTestCompile.exclude group: 'com.android.support', module: 'recyclerview-v7'
}
于 2016-02-09T00:11:56.520 回答