简体   繁体   中英

Android Espresso with Dagger

I am trying to run android instrument test with Espresso especially Double-Espresso library.

I know Espresso already has a dependency to dagger 1.2.1 and my app also use same dagger version. So I declared dependency like this.

// dagger-compiler already includes dagger.
compile 'com.squareup.dagger:dagger-compiler:1.2.1'

androidTestCompile 'com.google.guava:guava:16.0'
androidTestCompile 'javax.annotation:javax.annotation-api:1.2'
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9'
androidTestCompile('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.google.guava', module: 'guava'
    exclude group: 'com.squareup.dagger'
    //exclude group: 'javax.annotation', module: 'javax.annotation-api'
}

And my test look like this.

@LargeTest
public class IntegrationTest extends ActivityInstrumentationTestCase2<LoginSelectMethodActivity> {

    public IntegrationTest() {
    // This constructor was deprecated - but we want to support lower API levels.
        super("com.google.android.apps.common.testing.ui.testapp", LoginSelectMethodActivity.class);
    }
    @Override
    public void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    @LargeTest
    public void test_actionButtonEnableWhenInputsAreValid() {
        Espresso.onView(ViewMatchers.withId(R.id.signin_email))
                .perform(ViewActions.click());

        Espresso.onView(ViewMatchers.withText(R.id.et_email))
                .perform(ViewActions.typeText("valid@email.com"), ViewActions.pressKey(KeyEvent.KEYCODE_ENTER),
                        ViewActions.typeText("aGoodP455w0rd"), ViewActions.pressKey(KeyEvent.KEYCODE_ENTER));

        Espresso.onView(ViewMatchers.withId(R.id.b_signin))
                .check(matches(withText(R.string.sign_in)));
    }

}

However the test fails and the log says that it can't find one of dagger's methods.

08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object;
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0015
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.registerIdlingResources
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object;
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0009
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.registerLooperAsIdlingResource
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object;
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug I/dalvikvm﹕ Could not find method dagger.ObjectGraph.get, referenced from method com.google.android.apps.common.testing.ui.espresso.Espresso.setFailureHandler
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug W/dalvikvm﹕ VFY: unable to resolve virtual method 15010: Ldagger/ObjectGraph;.get (Ljava/lang/Class;)Ljava/lang/Object;
08-27 22:24:13.979    4074-4087/com.trumpia.android.loyaltea.debug D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
...
08-27 22:24:14.031    4074-4087/com.trumpia.android.loyaltea.debug I/TestRunner﹕ ----- begin exception -----
08-27 22:24:14.031    4074-4087/com.trumpia.android.loyaltea.debug I/TestRunner﹕ java.lang.NoSuchMethodError: dagger.ObjectGraph.get
            at com.google.android.apps.common.testing.ui.espresso.Espresso.onView(Espresso.java:51)

I run the test in Android Studio with GoogleInstrumentationTestRunner . Hopefully can anyone give me a little light for this.

You need to depend on dagger.

replace this:

// dagger-compiler already includes dagger.
compile 'com.squareup.dagger:dagger-compiler:1.2.1'

with this:

compile 'com.squareup.dagger:dagger:1.2.1'
provided 'com.squareup.dagger:dagger-compiler:1.2.1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM