5

我想在 gradle 中使用 espresso 测试。为此,我尝试使用Jake Wharton 的双份浓缩咖啡,但它不起作用。

我读过它可能是重复的依赖项,但我已经排除了 Dagger 和 Support-v4 库,就像 Jake 在他的自述文件中所写的那样。

谁能给我建议这里可能出了什么问题?

测试类:

package info.korzeniowski.myapp.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import info.korzeniowski.myapp.R;
import info.korzeniowski.myapp.activity.MainActivity_;

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.contrib.DrawerActions.closeDrawer;
import static com.google.android.apps.common.testing.ui.espresso.contrib.DrawerActions.openDrawer;
import static com.google.android.apps.common.testing.ui.espresso.contrib.DrawerMatchers.isClosed;
import static com.google.android.apps.common.testing.ui.espresso.contrib.DrawerMatchers.isOpen;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;

public class BasicTest extends ActivityInstrumentationTestCase2<MainActivity_> {

    public BasicTest() {
        super(MainActivity_.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    @LargeTest
    public void testOpenAndCloseDrawer() {
        onView(withId(R.id.drawer_layout)).check(matches(isClosed()));

        openDrawer(R.id.drawer_layout);
        onView(withId(R.id.drawer_layout)).check(matches(isOpen()));

        closeDrawer(R.id.drawer_layout);
        onView(withId(R.id.drawer_layout)).check(matches(isClosed()));
    }
}

构建.gradle:

buildscript {
    def androidGradlePluginVersion= '0.9.+'
    def androidAptVersion = '1.2+'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:${androidGradlePluginVersion}"
        classpath "com.neenbedankt.gradle.plugins:android-apt:${androidAptVersion}"
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}

apply plugin: 'android'
apply plugin: 'android-apt'


apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName 'info.korzeniowski.myapp'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        abortOnError false
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

def androidSupportVersion = '19.0.+'
def greenDaoVersion = '1.3.+'
def androidAnnotationsVersion = '2.7.1'
def daggerVersion = '1.2.1'

dependencies {
    // Android Support
    compile "com.android.support:support-v4:${androidSupportVersion}"
    compile "com.android.support:appcompat-v7:${androidSupportVersion}"

    // Android Annotations
    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"

    // Dagger
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"

    // Espresso
    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger', module: 'dagger'
    }
    androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r2') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
}

试验结果:

Running tests
Test running started

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.IllegalAccessError''. Check device logcat for details
Test running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'

日志猫:

3-18 12:52:12.168      274-443/system_process I/ActivityManager﹕ Start proc info.korzeniowski.myapp for added application info.korzeniowski.myapp: pid=2998 uid=10049 gids={50049, 1028}
03-18 12:52:27.417    2998-2998/? I/dalvikvm﹕ Failed resolving Lorg/hamcrest/integration/EasyMock2Adapter; interface 2417 'Lorg/easymock/IArgumentMatcher;'
03-18 12:52:27.417    2998-2998/? W/dalvikvm﹕ Link of class 'Lorg/hamcrest/integration/EasyMock2Adapter;' failed
03-18 12:52:27.537    2998-2998/? W/ClassPathPackageInfoSource﹕ Cannot load class. Make sure it is in your apk. Class name: 'org.hamcrest.integration.EasyMock2Adapter'. Message: org.hamcrest.integration.EasyMock2Adapter
    java.lang.ClassNotFoundException: org.hamcrest.integration.EasyMock2Adapter
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: org/hamcrest/integration/EasyMock2Adapter
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.integration.EasyMock2Adapter" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/info.korzeniowski.myapp.test-1.apk", zip file "/data/app/info.korzeniowski.myapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/info.korzeniowski.myapp.test-1, /data/app-lib/info.korzeniowski.myapp-1, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
03-18 12:52:27.537    2998-2998/? I/dalvikvm﹕ Failed resolving Lorg/hamcrest/integration/JMock1Adapter; interface 2468 'Lorg/jmock/core/Constraint;'
03-18 12:52:27.537    2998-2998/? W/dalvikvm﹕ Link of class 'Lorg/hamcrest/integration/JMock1Adapter;' failed
03-18 12:52:27.587    2998-2998/? W/ClassPathPackageInfoSource﹕ Cannot load class. Make sure it is in your apk. Class name: 'org.hamcrest.integration.JMock1Adapter'. Message: org.hamcrest.integration.JMock1Adapter
    java.lang.ClassNotFoundException: org.hamcrest.integration.JMock1Adapter
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: org/hamcrest/integration/JMock1Adapter
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.integration.JMock1Adapter" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/info.korzeniowski.myapp.test-1.apk", zip file "/data/app/info.korzeniowski.myapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/info.korzeniowski.myapp.test-1, /data/app-lib/info.korzeniowski.myapp-1, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:204)
            at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:88)
            at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:39)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:50)
            at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:47)
            at android.test.SimpleCache.get(SimpleCache.java:31)
            at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:72)
            at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
            at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
            at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
            at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
            at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
            at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
            at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:366)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
03-18 12:52:28.477    2998-3011/? I/TestRunner﹕ started: testOpenAndCloseDrawer(info.korzeniowski.myapp.test.BasicTest)
03-18 12:52:28.497      274-439/system_process I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN flg=0x10000000 cmp=info.korzeniowski.myapp/.activity.MainActivity_} from pid 2998
03-18 12:52:28.617    2998-2998/? I/greenDAO﹕ Creating tables for schema version 1
03-18 12:52:28.687    2998-2998/? W/dalvikvm﹕ Class resolved by unexpected DEX: Linfo/korzeniowski/myapp/MyApp;(0x416fc6a8):0x73054000 ref [Ldagger/ObjectGraph;] Ldagger/ObjectGraph;(0x416fc6a8):0x72ec8000
03-18 12:52:28.687    2998-2998/? W/dalvikvm﹕ (Linfo/korzeniowski/myapp/MyApp; had used a different Ldagger/ObjectGraph; during pre-verification)
03-18 12:52:28.697    2998-2998/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-18 12:52:28.738    2998-2998/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
            at info.korzeniowski.myapp.MyApp.onCreate(MyApp.java:32)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4444)
            at android.app.ActivityThread.access$1300(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
03-18 12:52:28.738      274-286/system_process W/ActivityManager﹕ Error in app info.korzeniowski.myapp running instrumentation ComponentInfo{info.korzeniowski.myapp.test/android.test.InstrumentationTestRunner}:
03-18 12:52:28.738      274-286/system_process W/ActivityManager﹕ java.lang.IllegalAccessError
03-18 12:52:28.738      274-286/system_process W/ActivityManager﹕ java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementationF
4

1 回答 1

9

由于在包中找不到类org.hamcrest.integration,请尝试将其排除。然后您的依赖项看起来像

// Espresso
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
    exclude group: 'com.squareup.dagger', module: 'dagger'
    exclude group: 'org.hamcrest', module: 'hamcrest-integration'
}
androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r2') {
    exclude group: 'com.android.support', module: 'support-v4'
    // Not sure whether line below is needed
    exclude group: 'org.hamcrest', module: 'hamcrest-integration'
}
于 2014-03-26T13:54:26.543 回答